简体   繁体   中英

What is the format of an HTTP request message generated by AJAX?

To clarify: I am interested in what the actual HTTP request message looks like (headers etc.), not the code used to create it. In other words, something like this:

GET /info.txt HTTP/1.1
Host: www.example.com
SpecialHTTPHeaderIndicatingAsynchRequest: 1
SpecialHTTPHeaderDefiningCallbackFunction: MyFunc
...

I am trying to work out exactly what happens when an HTTP server receives an asynchronous request: how does the server know it's asychronous, how is the call-back function passed and what response code does it give immediately (if any) to acknowledge the request (before serving a response to it). I have always presumed that an HTTP server must give some sort of immediate (ie synchronous) response to acknowledge the request; now I'm not so sure, however. (If it doesn't do this, how does the requesting system know that the request has been received and is being worked on, or doesn't it know this?)


The server has no idea if the request was sent from client with Synchronous or Asynchronous mode. Now let see inside the HTTP Request. I have made a small example with PHP and HTML/JS

client.html

<html>
    <head>
    <script src="http://code.jquery.com/jquery-1.9.1.min.js"></script>
    </head>
    <script>
        $.ajax({
          url: "/StackOverFlow/sof/server.php",
         }).done(function( data ) {
            console.log(data);
          });   
    </script>
<body>

</body>
</html>

server.php

<?php
    print json_encode(array("NAME" => "Factory", "VALUE" => "Constructor"));
?>

when i start my client.html in my browser and active firebug ( free Add-In Firefox) i can see the http request sent from the client, and http response that i got from the server
在此处输入图片说明

Now if we look into %Apache_Home%/logs/access.log wi will see that Apache handle the Ajax request in the same way if it was from browser request(POST/GET)

::1 - - [18/Nov/2014:22:35:22 +0100] "GET /StackOverFlow/sof/server.php HTTP/1.1" 200 40 "http://localhost/stackoverflow/sof/client.html" "Mozilla/5.0 (Windows NT 6.1; WOW64; rv:33.0) Gecko/20100101 Firefox/33.0"

Hope that helps :)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM