Reputation: 8836
I am using the post and get methods for Ajax calls, and have a general question. There are two methods I've seen people use on the web:
Both approaches work. I've included them below:
// Construct the POST URL by hand
queryStringDelimiter = "?";
settings.queryParam = "q";
$.post(settings.url + queryStringDelimiter + settings.queryParam + "=" + query, {}, callback, settings.contentType);
// Use the data param
$.post(settings.url, {q:query}, callback, settings.contentType);
Are there any situations where you would construct the URL and parameters by hand instead of using the built-in data parameter? Any advantages of one method over the other?
Upvotes: 1
Views: 960
Reputation: 80070
If you let jQuery concatenating the data in to the appropriately formatted string you...
Really, the only argument AGAINST using the data parameter is if you already have the data in a concatenated format.
Upvotes: 1
Reputation: 91565
I'd say the data approach is better since it formalizes the process and reduces the chances of producing errors while string building. Besides, the JQuery library will do the string building for you so its basically the same amount of work.
Upvotes: 4
Reputation: 186632
No reason I can think of why one would construct them by hand unless they didn't know of the data parameter if there's more than 1 or 2 parameters, it's also cleaner to keep them separated so if you have to loop through the data object and possibly modify some values you'd just iterate over the object instead of parsing a string manually.
Upvotes: 1
Reputation: 41858
If I am using a GET I tend to just construct the URL, but when using POST I use the data parameter.
I do it because it is closer to how I was doing ajax calls before jQuery, when I wrote everything myself.
Upvotes: 0