5
I have the following code:
var form = new FormData();
form.append('user',$("#user").val());
form.append('password',$("#password").val());
var data = JSON.stringify($(form).serialize());
but var 'data' is being null, look at the example: https://jsfiddle.net/e5p00evx/
The intention is to send this data using $.ajax:
$.ajax({
url: "http://localhost:29199/default.aspx?tipo=autenticar",
data: data,
cache: false,
contentType: false,
processData: false,
type: 'POST',
xhr: function () {
var myXhr = $.ajaxSettings.xhr();
if (myXhr.upload) {
myXhr.upload.addEventListener('progress', function (s) {
console.log('PROGRESSO', s);
}, false);
}
return myXhr;
},
success: function (a, b, c) {
console.log('SUCESSO', a, b, c);
},
error: function (a) { console.log('ERROR', a); }
});
On the server side I already have a structure to receive json and deserialize it in object.
The Formdata and the
serialize
are alternative. How to send this data to the server?– Sergio
You need what’s in Formdata before you send it?
– Sergio
Actually I want to send a form with only a parameter "query", this will have json converted into Base64, but for this I need to be able to convert the data from the initial form to json. form -> add all information to form; data -> convert form data to json; form -> add a "key" param with the value "data" convert to Base64;
– ISFO
Formdata’s idea is to be impervious to attempts to manipulate data on the client’s side. That is, when you insert something inside, you can’t take it out anymore. If that’s enough, use Formdata, otherwise you have to use Base64
.serialize()
but passing the element of the DOM and not the Formdata.– Sergio
I get it, being John is sad! rsrs... found the solution to my problem: https://jsfiddle.net/e5p00evx/2/ is much simpler than I expected, I needed to find some way to generate a json without concatenating strings. Thank you.
– ISFO
Then you should have an element
<form>
and then use something like this: https://jsfiddle.net/rek9brh0/– Sergio