Jump to content

XMLHttpRequest

From Wikipedia, the free encyclopedia
This is an old revision of this page, as edited by 2601:642:4600:d3b0:9111:ff68:b849:3d9b (talk) at 00:21, 9 April 2024 (XMLHttpRequest usage). The present address (URL) is a permanent link to this revision, which may differ significantly from the current revision.

XMLHttpRequest (XHR) is an API in the form of a JavaScript object whose methods transmit HTTP requests from a web browser to a web server.[1] The methods allow a browser-based application to make a fine-grained server call and store the results in XMLHttpRequest's responseText attribute.[2] The XMLHttpRequest class is a component of Ajax programming. Prior to Ajax, an HTML form needed to be completely sent to the server followed by a complete browser page refresh.[2]

History

The concept behind XMLHttpRequest was conceived in 2000 by the developers of Microsoft Outlook – available on the Windows 2000 operating system.[3] The concept was then implemented within the Internet Explorer 5 (2001) browser's interpreter.[a] However, the original syntax did not use the XMLHttpRequest identifier. Instead, the developers used the identifiers ActiveXObject("Msxml2.XMLHTTP") and ActiveXObject("Microsoft.XMLHTTP").[4] As of Internet Explorer 7 (2006), all browsers support the XMLHttpRequest identifier.[4]

The XMLHttpRequest identifier is now the de facto standard in all the major browsers, including Mozilla's Gecko layout engine (2002), Konqueror (2002), Safari 1.2 (2004),[5] Opera 8.0 (2005),[6], and iCab (2005).[7]

With the advent of cross-browser JavaScript libraries such as jQuery, developers can invoke XMLHttpRequest functionality indirectly.

Standards

The World Wide Web Consortium (W3C) published a Working Draft specification for the XMLHttpRequest object on April 5, 2006.[8] [b] On February 25, 2008, the W3C published the Working Draft Level 2 specification.[9] Level 2 added methods to monitor event progress, allow cross-site requests, and handle byte streams. At the end of 2011, the Level 2 specification was absorbed into the original specification.[10]

At the end of 2012, the WHATWG took over development and maintains a living document using Web IDL.[11]

XMLHttpRequest usage

Generally, sending a request with XMLHttpRequest has several programming steps.

  1. Create an XMLHttpRequest object by calling a constructor:
    var request = new XMLHttpRequest();
    
  2. Call the "open" method to specify the request type, identify the requested resource, and select asynchronous or synchronous operation:
    request.open('GET', 'https://example.com/products/12', true /* asynchronous */);
    
  3. Set an event listener that will be notified when the request's state changes:
    request.onreadystatechange = listener;
    
  4. Initiate the request by calling the "send" method:
    request.send()
    
  5. Respond to state changes in the event listener. If the server sends a response object, by default it is captured in the "responseText" property. When the object stops processing response data, it changes to state 4, the "done" state.
    function listener() {
       // Check whether the request is done and successful.
      if (request.readyState == 4 && request.status == 200)
        console.log(request.responseText); // Display the text.
    }
    

Aside from these general steps, XMLHttpRequest has many options to control how the request is sent and how the response is processed. Custom header fields can be added to the request to indicate how the server should fulfill it, and an object to upload in the request can be provided in the "send" call. The response can be automatically parsed from the JSON format into a readily usable JavaScript object, or processed gradually as it arrives rather than waiting for the entire text. The request can be aborted prematurely or set to fail automatically if not completed in a specified amount of time.

See also

References

  1. ^ Mahemoff, Michael (2006). Ajax Design Patterns. O'Reilly. p. 92. ISBN 978-0-596-10180-0. Javascript lacks a portable mechanism for general network communication[.] ... But thanks to the XMLHttpRequest object, ... Javascript code can make HTTP calls back to its originating server[.]
  2. ^ a b Mahemoff, Michael (2006). Ajax Design Patterns. O'Reilly. p. 92. ISBN 978-0-596-10180-0.
  3. ^ "Article on the history of XMLHTTP by an original developer". Alexhopmann.com. 2007-01-31. Archived from the original on 2009-01-30. Retrieved 2009-07-14. The reality is that the client architecture of GMail appears to follow the rough design of the Exchange 2000 implementation of Outlook Web Access for IE5 and later which shipped way back in 2000.
  4. ^ a b Mahemoff, Michael (2006). Ajax Design Patterns. O'Reilly. p. 93. ISBN 978-0-596-10180-0.
  5. ^ "Archived news from Mozillazine stating the release date of Safari 1.2". Weblogs.mozillazine.org. Archived from the original on 2009-06-02. Retrieved 2009-07-14.
  6. ^ "Press release stating the release date of Opera 8.0 from the Opera website". Opera.com. 2005-04-19. Retrieved 2009-07-14.
  7. ^ Soft-Info.org. "Detailed browser information stating the release date of iCab 3.0b352". Soft-Info.com. Retrieved 2009-07-14.
  8. ^ "Specification of the XMLHttpRequest object from the Level 1 W3C Working Draft released on April 5th, 2006". W3.org. Retrieved 2009-07-14.
  9. ^ "Specification of the XMLHttpRequest object from the Level 2 W3C Working Draft released on February 25th, 2008". W3.org. Retrieved 2009-07-14.
  10. ^ "XMLHttpRequest Editor's Draft 5 December 2011". w3.org. Retrieved 5 December 2011.
  11. ^ "XMLHttpRequest Standard/#specification-history".

Notes

  1. ^ Modern browsers execute JavaScript using a just-in-time compiler called a JavaScript engine.
  2. ^ The standard was edited by Anne van Kesteren of Opera Software and Dean Jackson of W3C.