Click to See Complete Forum and Search --> : what type of server side response to ajax request?


jazz_snob
01-13-2008, 02:50 AM
I hate javascript. Fortunately there is the prototype library. Now I'm hooked on ajax.

Presently, I'm having the server response be plain ol' HTML that I update some container (div, tr, whatever) with. But its limiting. For example, suppose the user is submitting a form and their could be a server-side error or it might succeed. So in one case I need to return the error message and display it. In the case of success I need to update some other part of the page w/ new data. I experimented w/ sending back some JSON code, but I ran into problems w/ Internet Exploder.

I just read this fine article:
http://www.quirksmode.org/blog/archives/2005/12/the_ajax_respon.html

So I would like to ask the phpbuilder.com community what approaches have they used successfully. As mentioned, I'm just sending back plain HTML. One thing I'm doing on 'success' is to prefix the HTML w/ the string '_success=' which the js code strips out before displaying. Without that string present the js code knows to update the error <div> w/ whatever the server returned.

Thanks!

Weedpacket
01-13-2008, 04:30 AM
If there's an error, then the server should respond with the appropriate HTTP status code (4xx for an error in what the client sent, 5xx for a stuff-up on the server); prototype's Ajax support can distinguish between error responses and successful ones (typically 200). Specify different handlers to deal with the different cases.

jazz_snob
01-21-2008, 12:52 AM
Besides a 404 or other comm. error, I was thinking about a situation where a user enters info into a form that is submitted as an ajax request, but the server-side verification determines there is an error. I need a way to send back a "flexible" response that I can parse and update the client display w/ the appropriate error message, or new data, etc.

I ultimately opted for XML reponse because I could structure it to contain all the needed elements, and just have javascript walk the document tree and determine if there was overall any error, or whether to show a success message, or what data to update the screen with. Something like this:

<?xml verison="1.0"?>
<ajax_resp>
<errors />
<messages>
<message>User added</message>
</messages>
<data>
<record id="4"><username>Some user</username></record>
</data>
</ajax_resp>

It was more work but is more flexible than just having server return HTML, so for now I'm hooked on the idea of sending back an XML response.

Weedpacket
01-22-2008, 03:27 AM
Well, a 400 if the request has bad syntax, maybe a 403 if they need to be logged in but aren't, and maybe a 409 if they're doing something that takes several steps and they've skipped a necessary one....

And then have more specific custom headers that the relevant handlers can check for details. Note also that the specification expects you to also provide content that would describe to the the user how to make suitable corrections to their request. That's where you would put the XML.

Sounds plenty flexible to me. At least as flexible as sending everything back with a 200 response code and then doing all the rest by hand. Like you said: "It was more work..."

Oh, and if you've just created a new user, then that can be a 201 response (with a link to something apropriate like the new user's profile page).