0

Using plain, native Javascript (no jQuery or other framework answers please), how does the response part of an AJAX call get processed and formatted before being sent back to the requesting script?

I have been looking for this answer and cannot seem to find it in the many searches I have performed. I would like to see an example, and not one focusing on the POST or GET variable access. I want to know how to respond to the script.

Jared Farrish
  • 48,585
  • 17
  • 95
  • 104
MountainMan
  • 753
  • 2
  • 10
  • 20
  • 1
    Your question makes no sense to me – Dr. Dan Dec 14 '12 at 05:10
  • Whatever you send from the client is wrapped up in a POST or GET request, and to get something back from the server all you really have to do is echo or return something in the PHP script, and it's returned to the XMLHttpRequests, if you have the right methods in place to catch it of course (simplified a lot). – adeneo Dec 14 '12 at 05:12
  • jQuery is a multi-browser JavaScript library designed to simplify the client-side scripting of HTML – NullPoiиteя Dec 14 '12 at 05:13
  • Read this question/answer: http://stackoverflow.com/questions/5485495/how-can-i-take-advantage-of-callback-functions-for-asynchronous-xmlhttprequest Short answer: Callbacks can accept JSON, XML, HTML (umm, sorta), plaintext. Nowadays you see a lot of people using JSON, because it's lightweight and you can use PHP's [`echo json_encode($arr);`](http://php.net/manual/en/function.json-encode.php) for example. XML and HTML in roll-your-own scripts (NOT jQuery...) can be trickier to get working (or at least back in the `MSXML2.XMLHttpRequest` it was). – Jared Farrish Dec 14 '12 at 05:16
  • @Dr.Dan Well Then! How about Jared Farrish's question? – MountainMan Dec 14 '12 at 05:27
  • Here's a fairly in-depth article including different examples of responding to an Ajax request with PHP: http://ajaxpatterns.org/XMLHttpRequest_Call – Jared Farrish Dec 14 '12 at 05:28
  • @Jared There is a difference between editing something and a wholesale rewriting. I am annoyed at that wholesale butchering rewriting. A Question YOU wrote and which makes sense to you is all fine, I see you are quite experienced or at least have build up a lot of rep, but please, what is the point of such a wholesale replacement of one question with another? – MountainMan Dec 14 '12 at 05:29
  • There is no need to be rude. If you see a difference between what he edited and your question's intent, edit it- but in any case it wasn't possible to tell what was being asked originally. – argentage Dec 14 '12 at 05:33
  • Revert it. The content on this site in the end belongs to the community, so there is a sense that what is in the questions and at times answers needs to be focused and clarified to make it an effective question or answer. There was a lot of noise and frankly, you were likely to get more hassle than help because the question was noisy and easily misread. But, you can revert it by clicking the link to the time above my name if it offends you that much or I got it that wrong. You won't hurt my feelings either way. – Jared Farrish Dec 14 '12 at 05:34
  • @airza Hmm.. Well, I could have I admit made the case instead of expressing frustration, that a question, with many flaws to an experienced coder, nevertheless might give many folk an idea of the areas the OP lacks understanding of, and tooheavy editing sorta defeats that. In any case, the link he provided looks like EXACTLY WHAT I HAD HOPED TO FINDF for a study resource. – MountainMan Dec 14 '12 at 05:36
  • (And although your english is good enough, we get a lot of users who struggle. We "wholesale rewrite" usually to make the question accessible, and to me this was no different.) – Jared Farrish Dec 14 '12 at 05:36
  • @Jared. Gotcha. No, no revert... and thanks. I go study now. – MountainMan Dec 14 '12 at 05:38
  • OMG it is better than most textbooks. I'll be a couple weeks soaking that up and practicing. – MountainMan Dec 14 '12 at 05:53

1 Answers1

0

In PHP:

header( 'Content-Type: application/json' );
$jsonResponse = array("response" => "Good job.");
echo json_encode($jsonResponse);

Part of an AJAX call is the function listening for the response - similar to how an HTTP request has your browser waiting for a webpage response. In this case the headers indicate the resulting data is in json format. If you do this totally by hand you will get a standardized text response which you will need to parse and package into javascript objects (Which you can do because javascript's associative array syntax for objects makes it straightforward to set object properties with names pulled from strings.)

argentage
  • 2,758
  • 1
  • 19
  • 28
  • ..So in the broadest sense, it is like the php script is POSTING, but not to another script per se, the – MountainMan Dec 14 '12 at 05:44
  • Yes. It isn't accurate to say that it's replying through a POST to the script, but that part of an AJAX request implies a return which is specified in PHP by setting the headers and echoing the response. – argentage Dec 14 '12 at 22:47