Uncomprehensible jQuery $.ajax() behavior when data contains consecutive question marks

jQuery uses ?? as a placeholder for the callback function when using jsonp. When it parses an Ajax request, and it finds the double questionmark (or more questionmarks), it automatically assumes that you are trying to use jsonp. When you set the content-type manually, it will ignore the questionmarks.

So, avoid the problem by using contentType:

$.ajax(
    url: "your-url.php",
    dataType: "json",                 // what you expect the server to return
    contentType: "application/json",  // what you are sending
    ...
);

For reference:

jQuery Bugtracker: $.AJAX CHANGED THE POST CONTENT IF INCLUDE "??" (2 QUESTION MARK)

Hopes it saves someone else from hours of debugging...


If you're not going to format the "data" value as a valid HTML query string, you shouldn't pre-stringify it. As you noted, if you don't call "JSON.stringify()" then it works. That's because the library already knows to handle that for you.

Now, if you want to send your JSON string as a parameter itself to server side code that expects to decode some JSON, then you need to set it up as a parameter:

    $.ajax(url, {
      // ...
      data: { jsonParam: jsonData },
      // ...
    });

Now your server will see an HTTP request with a parameter called "jsonParam", and its value will be your JSON-stringified string.


I think the answer you're looking for is setting jsonp:false in the AJAX call options. I had this exact same problem and fixed it by doing that.

Read the answer to this similar question for more details: Post data being overriden since upgrading from jQuery 1.4 to 1.5