More AJAX POST over GET
July 8, 2006
/ Filed under: JavaScript, Web Development
Yesterday I mentioned how it seems that AJAX "POST" requests are handled better, meaning invalid characters don’t have to be "checked for," and replaced as necessary. Well, it turns out I was wrong. This is becoming more convoluted than I originally thought, so to make things more clear, I’ll provide some better examples. I’m also "learning as I go," so forgive the "incompleteness" of this all - when I learn something, I post it. Turns out, with AJAX, I’m learning something new every day, and often times it goes against what I previously said. I apologize for that - it’s all just trial-and-error for me. A "GET" request in AJAX submits the variables (parameters) via a URL string, much like this:
A "POST" request in AJAX submits the variables (parameters) via a similar string, but not appended to a specific page. The variables (parameters) just float on their own:
Notice how mypage.php? is not in the list of parameters above, for a "POST" request. Rather, in an AJAX "POST" request, the actual page ( So, to make a long story short (and hopefully understandable), we’re still going to the same page ( So what is the problem?The problem is the variable values sent across (via the xmlHttpRequest object). With "GET," you have to watch out for invalid URL characters, such as the pound sign (#), the ampersand (&), and a few others. You have to find and replace those invalid characters, so your parameters can be sent successfully. With "POST," I’m just beginning to notice (despite my thoughts yesterday), that weird things are happening, as well. For example, with "POST," all spaces are removed from any string of text. So, if a user submits the following text into a form field:
... the "submitted value" will come across like this:
Ew. How can one possibly work with that? Also, I’ve noticed with "POST" requests that commas are rejected, too. Commas!? Why are commas rejected? Nothing should be rejected - we are submitting via "POST!" You can see my aggravation. I’m having to find/replace certain characters on the JavaScript side, before I submit the xmlHttpRequest. This is not how I want to spend a Saturday morning. I should be done with this by now.
Comments/Mentions
|
Editor Picks
Email NewsletterSubscribe to the digest newsletter to receive posts by email: Recent Comments
Advertisements
|
You’d add this line before sending POST data.
xmlhttp.setRequestHeader(’Content-Type’, ’application/x-www-form-urlencoded;’);