0

I have the following code which I post a bunch of JSON data to an ASHX file where I will process this data. Somehow the JSON is encoded and I have no clue what encoded it.

 $.ajax({        
    url: '/save_objects_channels.ashx',
    data: jsonParams,
    contentType: 'application/json',
    dataType: 'json',                                
    success: function(data) {                                  

    },
    error: function (xhr, ajaxOptions, thrownError){

    },
    complete: function() {

    }
});

Here is my sample json that I posted (I generate this as string):

var jsonParams = '[ { objectID: 333, channelID: 3, supplierId: 2, checked: true },{ objectID: 444, channelID: 4, supplierId: 5, checked: true } ]';

enter image description here

2
  • Why exactly is that a problem? Commented Jun 6, 2011 at 12:31
  • the encoded part, that is causing problems Commented Jun 6, 2011 at 12:32

2 Answers 2

4

jQuery encoded it. You chose to send it as a GET request (which is the default for .ajax()), which transfers all data in the URL as part of the query string. As Clement Herreman also points out, the query string must be encoded.

You might want to switch to type: "POST" in your .ajax() parameters.

GET requests have a length limit that can bite you when the JSON string gets longer. POST requests have virtually no size limit.

Plus, you will cause a data leak: Query strings are written to the web server logs, possibly sensitive data could end up there when you are not careful. POST requests are logged, too. But their payload will not be logged, as it is not part of the URL.

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

3 Comments

Are you sure jQuery does it ? It thought they decided not to do it, to avoid double encoding bugs.jquery.com/ticket/6834
@Clement: Yes, it does, I'm absolutely sure. Try it.
@Clement: I've looked at ajax() in the jQuery source and what it does is calling the param() support function which encodes the data.
3

Because URL must be encoded, according the RFC 3986

Hint on how to encode url using Javascript : Encode URL in JavaScript?

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.