Page side javascript:
var model = {"NumberOfCPUs":"2","NumberOfCores":"4","OSType":"Linux","OSVersion":"???"};
var id = 0;
var success = false;
//send to server:
$.ajax(
{
async: false,
data: JSON.stringify(model),
dataType: "json",
type: "post",
url: serverSideURL
});
The contents of the request that actually gets sent to the server:
{"NumberOfCPUs":"2","NumberOfCores":"4","OSType":"Linux","OSVersion":"jQuery19107363727174233645_1373301489648?"}
What is happening? Is the string "???" some sort of special control word or something in jQuery?
Thanks.
If I read through the comments of the bugs in the other answers, the real problem lies in the fact that you stringify your json-string before passing it to data
. data
expects either a String
containing a query (e.g. "a=asdf&b=qwer"
or an Object
containing the keys and values that it will turn into a query). You are instead passing something like '{"a":"asdf","b":"qwer"}'
, a String
containing a stringified array that is not a query. It somehow manages to convert in data the server understands, but apparently triggers that bug/feature too.
Solution 1
If you want to access the data via $_POST['a']
to get key "a"
in your JSON-object:
$.ajax( {
async: false,
data: model, //Pass the Array itself to the function
dataType: "json",
type: "post",
url: serverSideURL
});
(Source; Cowboy on jQuery bugs)
Solution 2
If you want to retrieve the JSON-string:
$.ajax( {
async: false,
data: {
"MyLilString": JSON.stringify( model )
}
dataType: "json",
type: "post",
url: serverSideURL
});
In this case $_POST["MyLilString"]
contains the serialized JSON, which you can use json_decode()
on.
(Source; Ajpiano on jQuery bugs)
Another sugestion
Another suggestion I did extract from the comments (but I am now unable to find -_-') is to set jsonp
to false
.
$.ajax( {
async: false,
data: model
dataType: "json",
type: "post",
url: serverSideURL,
jsonp: false
});
This should stop jQuery from inserting the callback function into the request body too.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With