3

In following this post, I wanted to convert all my Ajax requests to JSON automatically and it's been working fine.

However, I've noticed that sometimes jQuery adds a mysterious &[object%20Object] at the end of the URL right before the anti-cache parameter (_=1472476048876 for example), because I also have cache: false set.

When I change processData to be true, the mysterious part disappears. Anyone have any ideas why? I'm using jQuery 2.1.4.

Edit: I've noticed that the error only is produced when I make an Ajax call via Datatables .load() API. Has anyone else had this issue?

Edit 2: In response to @war10ck 's great answer, to clarify, I'm making a GET request but I'm not sending any data with it. I create the URL by hand and thus I think that Datatables is adding some data (even an empty object maybe) to my request which is getting turned into [object%20Object].

3 Answers 3

2

I think I found the root of the problem. Deep in the internals of Datatables, in a function called _fnBuildAjax, even if you don't give anything as a data parameter, there are two lines that do the following:

var tmp = {};
// ...if data is empty...
data = tmp;

This ends up passing an empty object to the jQuery ajax call further down near the bottom of the same function. Normally jQuery would take this empty object and not change the call, but since processData is false, jQuery appends it to the URL (being a GET request after all) and turns it into [object%20Object] even though it is an empty object. This only happens with Datatables because elsewhere in my code I am giving simply not specifying a data parameter for an Ajax call and jQuery is smart enough simply not to append anything to the URL, but when it sees an empty object it doesn't know what to do and appends it as is. I hope this helps future programmers debug this edge case :). Thanks again to @war10ck for his explanation.

Sign up to request clarification or add additional context in comments.

1 Comment

Well researched! +1 That's an interesting find. I feel like that could be handled better and/or differently in the future. You may want to submit a request to the developer(s) to get his/her thoughts on it.
1

It sounds like you're making a GET request, whether via your own jQuery call or via Datatables. In a GET request, parameters are sent to the server via the url query string:

example.com?key1=value1&key2=value2

Part of the abstraction that the jQuery does behind the scenes in $.ajax() is to convert the data that you pass in via the data parameter in the object to a suitable format for the request. In the case of a standard GET request with content type application/x-www-form-urlencoded, it's a string of key/value paris separated by ampersands. This works great when the data you're working with in your application is a JavaScript object. As an example:

var obj = {
    key1: value1,
    key2: value2
};

$.ajax({
    type: "GET",
    url: "www.example.com",
    data: obj
});

Results in the called url being:

www.example.com?key1=value1&key2=value2

as jQuery does the data conversion behind the scenes for you. However, according to the docs:

data parameter:

Data to be sent to the server. It is converted to a query string, if not already a string. It's appended to the url for GET-requests. See processData option to prevent this automatic processing. Object must be Key/Value pairs. If value is an Array, jQuery serializes multiple values with same key based on the value of the traditional setting (described below).

and

processData parameter:

By default, data passed in to the data option as an object (technically, anything other than a string) will be processed and transformed into a query string, fitting to the default content-type "application/x-www-form-urlencoded". If you want to send a DOMDocument, or other non-processed data, set this option to false.

setting processData to false will prevent the default data manipulation and conversion from an Object format to a suitable url String. Instead, the data will be converted using the .toString() native method on the Object prototype by the browser automatically when the url is called. Here is how that appears:

var obj = {};
alert(obj.toString());

So now, since the default processing is off, your data example becomes the following:

var obj = {
    key1: value1,
    key2: value2
};

$.ajax({
    type: "GET",
    url: "www.example.com",
    data: obj,
    processData: false
});

Results in the called url being:

www.example.com?[object%20Object]

like you see above in the mini-code snippet with the extra addition of %20 as the url encoding value of a space, as noted in RFC 3986 and updated by RFC 6874 and RFC 7320.

Edit to elaborate on why Datatables is causing the above issue:

To further address your question of why it only appears to happen with .load(), it appears the datatables ajax object is a wrapper for jQuery's default $.ajax(). In the docs for $.ajax() the type parameter is defaulting to GET when not directly specified. So datatables initiates a GET request and, with processData set to false, the above scenario happens.

1 Comment

Thank you for the detailed answer, it's a great help. I do already understand all that you have said, but my problem is that there is no data being sent with the request and I create the URL by hand so there should be nothing added. You are right, in your example about obj becoming [object%20Object] but the question is: where is the data coming from?
0

Thank you guys! In addition to your researches that helped me to solve my problem I would like to share the corresponding solution.

In my case the problem is a call similar to

http://server/dir/resource.en.json?_=1510566367817

resulted in http://server/dir/[object%20Object]?_=1510566367817

The main hint is daveslab' one regarding to handling of empty objects.

In combination with a multi language project envinronment I came around thinking about missing resource files. That ist where the empty object joins the game because the relevant resource.fr.json file was missing.

In this case it doesn't matter if processData is set to false or true. I checked both.

Summary

Json files called by client-side code but missing on the server can cause the same issue.

Client facts in my case: I use jquery 3.2.1 AJAX Get-Service with the following configuration

{
    dataType: "json",
    async: true,    
    cache: false,
    processData: false 
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.