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

Feedback

Input format: The editor controls below will assist with Markdown syntax.

Status

Sub-status

Your info

You’d add this line before sending POST data. xmlhttp.setRequestHeader(’Content-Type’, ’application/x-www-form-urlenc ... Read more.

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 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.

Contact Matt

Similar Entries

Stats

482 unique visits since August 2008

Syndicate

Advertisements