More AJAX POST over GET Jul08 '06
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:
mypage.php?var1=tree&var2=house&var3=sky
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:
var1=tree&var2=house&var3=sky
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 (mypage.php) is still specified - but a little further down in the code - in the actual send() method of the xmlHttpRequest object.
So, to make a long story short (and hopefully understandable), we’re still going to the same page (mypage.php), and we’re still sending the same parameters.
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:
I like trees next to houses
... the "submitted value" will come across like this:
Iliketreesnexttohouses
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.
Categories: JavaScript
, Web Development ![]()
Add Feedback (view all)
Leave feedback
I'm having the same aggrivating issue with the POST request. The most annoying part is that i cannot possibly pass what i am doing through an ajax ... Read more.
Nm, thank you i just read your next post there boss... thanks, you don't know how much of a life saver that is... i totally forgot about encodeURI( ... Read more.
Mark, make sure to read Jennifer's feedback reg ... Read more.
encodeURIComponent() Helped me alot thanks!! it replaces naughty &?# and stuff for %[number] things. ... Read more.
encodeURIComponent solved my problem with & + and % signs. i am new here and tnx very much :) ... Read more.
Heh, looks like I'm on the same AJAX path as you exactly 2 years later... thanks for the blog entry! ... Read more.
matthom
is published and produced by Matt Thommes - an independent publishing enthusiast, mobile blogger, content creator, informative writer, web developer from a suburb of Chicago.
Never one to conform, Matt intends to promote the effect the web has on our lives, in an effort to intensify, instruct, and clarify all that is happening around us.
Popular Pages
- Fast rounded corners in Photoshop (7875 recent visits)
- PHP – passing variables across pages (2860 recent visits)
- JavaScript set selected on load (2346 recent visits)
- Removing all child nodes from an element (1704 recent visits)
- iPod songs out of order? (1412 recent visits)
- Firefox 3 smart address bar: wildcard search (1300 recent visits)
- Britney - Everytime piano tab (1180 recent visits)
- MySQL LEFT JOIN syntax (971 recent visits)
- Breathe Me - Sia (824 recent visits)
- Tumblr: how blogging should be (727 recent visits)
Similar Entries
- AJAX/Andy/Matt meeting review (1 recent visits)
- AJAX select list issue in IE6 (93 recent visits)
- AJAX requests in web site template (24 recent visits)
- Invalid characters for AJAX script (45 recent visits)
- Scary AJAX error (107 recent visits)
- AJAX meeting brief 1 (3 recent visits)
Stats
152 unique visits since August 2008
Recent Referrers (click)
- ajax post variables
- ajax post variable
- ajax read post parameter
- ajax post + sign problem
- ajax get post
- ajax get post
- ajax get post
- ajax get vs post
- ajax get POST
- ajax passing variable via method post
- http://matthom.com/archive/200
- AJAX variables with "+" sign
- AJAX variables with "+" sign
- AJAX variables with "+" sign
- ajax get or post
- +javascript +XMLHttpRequest + "post variables"
- ajax post request
- ajax post var
You’d add this line before sending POST data. xmlhttp.setRequestHeader(’Content-Type’, ’application/x-www-form-urlenc ... Read more.