JQuery is a great JavaScript framework that makes web developer life much easier. But like all framework, you need to learn its gotchas in order to work effectively with it.
Here is one of the gotchas. Jquery POST method lets you create Ajax HTTP POST request to the server. It is actually a shorthand to the JQuery Ajax method:
$.ajax({
type: "POST",url: "save.php",
data: "param1="+paramValue1
+"¶m2=paramValue2",
complete: function(){ }, //manage the complete if needed
success: function(){}}//get some data back to the screen if needed
});
Today, I'm gonna tell you how to solve the problem when JQuery only send partial data to AJAX Post. Maybe you've found that when executing the AJAX call, only part of the data is passed to the server and the rest vanishes. You usually see that some or all of the parameters you tried to pass are missing or cut in the middle. It's because JQuery uses ‘&’ as a separator between the parameters. If you have a ‘&’ within your key or value parameters, then the JQuery AJAX request gets really messed up.
You can solve this problem by encoding the parameters, replace & with %26 which is the standard encoding for that character. There are 2 ways to encode the parameters:
Semi-Automatic
Use .replace(/&/g, “%26″) –
Here is a working example:
$.ajax({
type: "POST",url: "save.php",
data: "param1="+paramValue1.replace(/&/g, "%26")
+"¶m2=paramValue2.replace(/&/g, "%26")",
complete: function(){ }, //manage the complete if needed
success: function(){}}//get some data back to the screen if needed
});
Fully Automatic
A more elegant way is to slightly change the way we call the meethod and let JQuery do that encoding for you –
Here is a working example:
$.ajax({
type: "POST",url: "save.php",
data: { "param1": paramValue1,
"param2": paramValue2 },
complete: function(){ }, //manage the complete if needed
success: function(){}//get some data back to the screen if needed
});
AJAX with Free ASP.NET Hosting
Try our AJAX with Free ASP.NET Hosting today and your account will be setup soon! You can also take advantage of our Windows & ASP.NET Hosting support with Unlimited Domain, Unlimited Bandwidth, Unlimited Disk Space, etc. You will not be charged a cent for trying our service for the next 3 days. Once your trial period is complete, you decide whether you'd like to continue.