MattEvansDev
MattEvansDev

Reputation: 665

Submitting jQuery.ajax data with string containing "???" it changes value to "jQuery19107363727174233645_1373301489648?"

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.

Upvotes: 8

Views: 2493

Answers (3)

Sumurai8
Sumurai8

Reputation: 20737

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.

Upvotes: 11

Ryan
Ryan

Reputation: 6517

It's a known bug:

When posting AJAX and the data has "??" is formats it to jQuery?

It's marked fixed even though in the comments on the bug it's clearly never been fixed. The issue should be reopened.

Workarounds apparently include changing your JSON line to the following:

var model = {NumberOfCPUs:"2",NumberOfCores:"4",OSType:"Linux",OSVersion:"???"};

and allowing it to be serialized. This way it won't trigger the regex that's causing the double question marks to be replaced.

Upvotes: 0

Ilya
Ilya

Reputation: 29693

?? is used as the callback name placeholder in the data for jsonp requests. So, finding it, jQuery will "promote" your request as jsonp.

jquery bug tracker, ticket #12326

Use this property

contentType: "application/json; charset=utf-8"

Upvotes: 3

Related Questions