I need to serialize all inputs from a form into a JSON string.
With the help of this post, I can successfully create a valid string as below:
{"input01":"value01","input02":"value02","input03":"value03"}
However, when I try to use the string to POST data using jQuery's Ajax function, it seems to add backslashes to the string, resulting in the JSON string being sent using GET rather than POST.
The loaded PHP page returns a $_GET
array of:
[{\"input01\":\"value01\",\"input02\":\"value02\",\"input03\":\"value03\"}] =>
I have tested the JSON string using alert()
to confirm the structure is correct before being used in the AJAX function.
Additionally, if I just manually type in the valid JSON string, the AJAX posts the data correctly.
My code is as follows:
var dataJSON = $.toJSON($('#form').serializeObject());
alert(dataJSON);
$.ajax({
type: "POST",
url: "ajax.php",
data: 'Query01=01&Query02=02',
dataType: 'json',
success: function(data){
if (data==1){
$('#wrap').load('ajax.php',dataJSON);
}
}
});
.ajax()
, then making another ajax request with.load()
. Is that correct? – Phlogistic