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.

13
  • 1
    Your question makes no sense to me
    – Dr. Dan
    Commented Dec 14, 2012 at 5: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
    Commented Dec 14, 2012 at 5:12
  • jQuery is a multi-browser JavaScript library designed to simplify the client-side scripting of HTML Commented Dec 14, 2012 at 5:13
  • Read this question/answer: stackoverflow.com/questions/5485495/… 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); 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). Commented Dec 14, 2012 at 5:16
  • @Dr.Dan Well Then! How about Jared Farrish's question? Commented Dec 14, 2012 at 5:27

1 Answer 1

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

2
  • ..So in the broadest sense, it is like the php script is POSTING, but not to another script per se, the <script type="text/Javascript" but to PARTICULARLY the AJAX request, and not via a POST, but through a header? Thanks, way ahead of a hundred Google resuts, right there. Commented Dec 14, 2012 at 5: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
    Commented Dec 14, 2012 at 22:47

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.