Avoid sending Ajax request parameters in query string style

Amr Elgarhy
TreeNodes
Published in
1 min readJun 29, 2011

While reviewing some code, I found someone calling jQuery Ajax like this:

$.ajax({
type: “POST”,
url: “/AddPost”,
data: “postText=”+ PostText ,
dataType: “json”,
success: function (status) {

}
}); //$.ajax({

And as you see in the previous code, it is sending the data using this line: data: “postText=”+ PostText ,

This is like a query string style, this will work well but image the user inserted a special char in PostText, such as ?? you will get on the server un expected value “”jQuery15107041011152323335_1309357585354"”

So never use this style while dealing with Ajax calls, you need to post it in JSON style like this:

$.ajax({
type: “POST”,
url: “/AddPost”,
data: { postText: PostText },
dataType: “json”,
success: function (status) {

}
}); //$.ajax({

This will work whatever char the user will insert.

--

--