Questions tagged [xmlhttprequest]

XMLHttpRequest (XHR) is a JavaScript object that exposes an API for making asynchronous HTTP requests from frontend code running a Web browser — that is, for enabling the programming technique known as AJAX. The XHR API is a legacy API. It has been superseded by the Fetch API.

Example usage

function reqListener () {
      console.log(this.responseText);
}
    
var oReq = new XMLHttpRequest();
oReq.addEventListener("load", reqListener);
oReq.open("GET", "http://www.example.org/example.txt");
oReq.send();

Taken from mozilla.org.

See also

11269 questions
128
votes
2 answers

Sending a JSON to server and retrieving a JSON in return, without JQuery

I need to send a JSON (which I can stringify) to the server and to retrieve the resulting JSON on the user side, without using JQuery. If I should use a GET, how do I pass the JSON as a parameter? Is there a risk it would be too long? If I should…
Jérôme Verstrynge
  • 51,859
  • 84
  • 263
  • 429
126
votes
6 answers

file_get_contents("php://input") or $HTTP_RAW_POST_DATA, which one is better to get the body of JSON request?

file_get_contents("php://input") or $HTTP_RAW_POST_DATA - which one is better to get the body of JSON request? And which request type (GET or POST) should I use to send JSON data when using client side XmlHTTPRequest? My question was inspired from…
Manuel Bitto
  • 4,777
  • 6
  • 37
  • 45
123
votes
6 answers

jQuery has deprecated synchronous XMLHTTPRequest

Like many others, my website is using jQuery. When I open the developer tools, I see a warning that says that XMLHTTPRequest is deprecated because of its detrimental effects to the end user's experience. I went on and read part of the…
Edd
  • 1,874
  • 3
  • 20
  • 29
116
votes
5 answers

Prevent redirection of Xmlhttprequest

Is it possible to prevent the browser from following redirects when sending XMLHttpRequest-s (i.e. to get the redirect status code back and handle it myself)?
Zim
  • 1,413
  • 2
  • 10
  • 8
116
votes
3 answers

HTTP 401 - what's an appropriate WWW-Authenticate header value?

The application I'm working on at the moment has a session timeout value. If the user hasn't interacted for longer than this value, the next page they try to load, they will be prompted to log in. All requests made are routed through this mechanism,…
Will Morgan
  • 4,285
  • 5
  • 26
  • 40
113
votes
5 answers

What do the different readystates in XMLHttpRequest mean, and how can I use them?

XMLHttpRequest has 5 readyStates, and I only use 1 of them (the last one, 4). What are the others for, and what practical applications can I use them in?
Marius
  • 54,363
  • 28
  • 121
  • 143
112
votes
8 answers

Add a "hook" to all AJAX requests on a page

I'd like to know if it's possible to "hook" into every single AJAX request (either as it's about to get sent, or on events) and perform an action. At this point I'm assuming that there are other third-party scripts on the page. Some of these might…
Yuliy
  • 16,249
  • 6
  • 39
  • 46
111
votes
6 answers

What is the cleanest way to get the progress of JQuery ajax request?

In plain javascript is very simple: need just to attach the callback to {XMLHTTPRequest}.onprogress var xhr = new XMLHttpRequest(); xhr.onprogress = function(e){ if (e.lengthComputable) var percent = (e.loaded / e.total) *…
guari
  • 3,496
  • 3
  • 26
  • 23
111
votes
8 answers

What's the best way to retry an AJAX request on failure using jQuery?

Pseudo code: $(document).ajaxError(function(e, xhr, options, error) { xhr.retry() }) Even better would be some kind of exponential back-off
Tom Lehman
  • 75,197
  • 69
  • 188
  • 262
110
votes
20 answers

XMLHttpRequest status 0 (responseText is empty)

Cannot get data with XMLHttpRequest (status 0 and responseText is empty): xmlhttp=new XMLHttpRequest(); xmlhttp.open("GET","http://www.w3schools.com/XML/cd_catalog.xml", true); xmlhttp.onreadystatechange=function() { if(xmlhttp.readyState==4) …
arigasa
  • 1,101
  • 2
  • 7
  • 3
105
votes
3 answers

XMLHttpRequest module not defined/found

This is my code: var XMLHttpRequest = require("xmlhttprequest").XMLHttpRequest; var xhr = new XMLHttpRequest(); xhr.open("GET", "//URL") xhr.setRequestHeader("Content-Type: application/json", "Authorization: Basic //AuthKey"); xhr.send(); I am…
wmash
  • 3,144
  • 3
  • 21
  • 53
96
votes
6 answers

Does an HTTP Status code of 0 have any meaning?

It appears that when you make an XMLHttpRequest from a script in a browser, if the browser is set to work offline or if the network cable is pulled out, the request completes with an error and with status = 0. 0 is not listed among permissible HTTP…
Mark Lutton
  • 6,567
  • 6
  • 36
  • 52
93
votes
9 answers

Handling cookies in PhoneGap/Cordova

I'm working on a PhoneGap app with server session usage. It needs cookies to handle the session. Additionally, the cookie from the load balancer should be handled, too. So there is no way around. How do you handle Cookies in your PhoneGap app? I…
Bernd
  • 1,091
  • 2
  • 9
  • 7
93
votes
13 answers

Uploading multiple files using formData()

var fd = new FormData(); fd.append("fileToUpload", document.getElementById('fileToUpload').files[0]); var xhr = new XMLHttpRequest(); xhr.open("POST", "uph.php"); xhr.send(fd); uph.php: var_dump($_FILES['fileToUpload']); This works, but obviously…
jaanisk
  • 931
  • 1
  • 6
  • 4
93
votes
10 answers

enabling cross-origin resource sharing on IIS7

I recently ran into with posting Javascript requests to another domain. By default XHR posting to other domains is not allowed. Following the instructions from http://enable-cors.org/, I enabled this on the other domain.
Andrew
  • 1,546
  • 1
  • 15
  • 20