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

12041 questions
136
votes
4 answers

Is onload equal to readyState==4 in XMLHttpRequest?

I am confuse about the xhr return event, as I can tell, there are not so much different between onreadystatechange --> readyState == 4 and onload, is it true? var xhr = new XMLHttpRequest(); xhr.open("Get", url, false); xhr.onreadystatechange =…
Huang
  • 1,919
  • 3
  • 15
  • 11
132
votes
9 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
  • 17,381
  • 6
  • 41
  • 47
132
votes
6 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,573
  • 2
  • 10
  • 8
131
votes
17 answers

AngularJS Error: Cross origin requests are only supported for protocol schemes: http, data, chrome-extension, https

I have three files of a very simple angular js application index.html
user3422637
  • 3,967
  • 17
  • 49
  • 72
128
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
  • 4,032
  • 3
  • 31
  • 69
128
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,948
  • 4
  • 21
  • 29
125
votes
2 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,470
  • 5
  • 29
  • 42
125
votes
9 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
  • 85,973
  • 71
  • 200
  • 272
119
votes
21 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,191
  • 2
  • 8
  • 3
116
votes
7 answers

fetch - Missing boundary in multipart/form-data POST

I want to send a new FormData() as the body of a POST request using the fetch api The operation looks something like this: var formData = new FormData() formData.append('myfile', file, 'someFileName.csv') fetch('https://api.myapp.com', { …
James
  • 4,927
  • 3
  • 22
  • 27
116
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,715
  • 3
  • 28
  • 25
114
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
  • 57,995
  • 32
  • 132
  • 151
109
votes
8 answers

loading json data from local file into React JS

I have a React component and I want to load in my JSON data from a file. The console log currently doesn't work, even though I'm creating the variable data as a global 'use strict'; var React = require('react/addons'); // load in JSON data from…
Desmond
  • 1,656
  • 3
  • 22
  • 34
106
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,959
  • 7
  • 41
  • 57
101
votes
8 answers

How can I send the "&" (ampersand) character via AJAX?

I want to send a few variables and a string with the POST method from JavaScript. I get the string from the database, and then send it to a PHP page. I am using an XMLHttpRequest object. The problem is that the string contains the character & a few…
candino
  • 1,297
  • 3
  • 11
  • 13