XHR/XDR/Fetch notes

From Helpful
Jump to: navigation, search
Related to web development, hosting, and such: (See also the webdev category)
jQuery: Introduction, some basics, examples · plugin notes · unsorted

Server stuff:

Dynamic server stuff:

This article/section is a stub — probably a pile of half-sorted notes, is not well-checked so may have incorrect bits. (Feel free to ignore, fix, or tell me)

XmlHttpRequest

(Interesting) object members/functions

These days, you probably won't use the constructor, open() or send() as you'll use an AJAX library instead.

If you do a little manual handling, the following may still be interesting:

  • readyState
  • status and/or statusText
  • setRequestHeader(header, value);
  • abort()
  • responseText or responseXML
  • getAllResponseHeaders() or getResponseHeader(header);


readyState

readyState:

  • 0: Uninitialized/Unsent (initial value, and set when manually abort()ed)
  • 1: Opened (open() was successful)
  • 2: Sent/Headers received: UA completed request, response headers received, waiting for response data)
  • 3: Loading/Receiving: and immediately before receiving message body chunks (may be called several times, and happens only after receiving headers)
  • 4: Loaded/Done: All data received

status and statusText

status (an int) and statusText (a string) must reflect the HTTP status when set.

They must be available when readystate is 3 or 4; if not, should be unavailable and access should (!) raise an exception. Unavailability may also be caused by the UA never being able to parse a status out of the response.

See also


Related errors

Data is null

Context being AJAX call in firefox (for me in jQuery and with getJSON, but it probably applies to all AJAX and more libraries).


One of the causes is that the fetch is against security policy (probably the same-source policy). These rules vary between browsers, which may make this look like a bug.


Suggestions:

  • make the URL refer to the same domain (host? port? It may even just be tripping over the fact that you're using an IP instead of name to the same host)
  • use build a proxy script
  • if the source is necessarily remote, try using a <script> block that has a call that hooks into your page (JSONP-style)


See also:


XMLHttpRequest cannot load 'URL'. No 'Access-Control-Allow-Origin' header is present on the requested resource

This article/section is a stub — probably a pile of half-sorted notes, is not well-checked so may have incorrect bits. (Feel free to ignore, fix, or tell me)

Short version: You are doing a request from a page from siteA to a resource on siteB, and siteB does not actively say that's okay. Which is what #CORS prevents and fixes.


XDomainRequest

Basically an IE drop-in for XHR with pre-standard CORS.

IE8 and IE9 only. Not present in IE10 or IE11, or any other browser.

It's perhaps useful for libraries to get CORS behaviour in wider set of browsers, but not to use directly.

Fetch

Fetch is intended as a modern variant of XHR, standardizing interplay with various other things that happened since XHR got defined. See e.g. [1]

Not supported in IE, supported in Edge.