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
3020
votes
34 answers

How can I upload files asynchronously?

I would like to upload a file asynchronously with jQuery. $(document).ready(function () { $("#uploadbutton").click(function () { var filename = $("#file").val(); $.ajax({ type: "POST", url:…
Sergio del Amo
  • 71,609
  • 66
  • 148
  • 177
1150
votes
32 answers

Access-Control-Allow-Origin Multiple Origin Domains?

Is there a way to allow multiple cross-domains using the Access-Control-Allow-Origin header? I'm aware of the *, but it is too open. I really want to allow just a couple domains. As an example, something like this: Access-Control-Allow-Origin:…
Thomas J Bradley
  • 11,764
  • 3
  • 15
  • 8
582
votes
13 answers

Send POST data using XMLHttpRequest

I'd like to send some data using an XMLHttpRequest in JavaScript. Say I have the following form in HTML:
Jack Greenhill
  • 8,952
  • 11
  • 37
  • 67
573
votes
17 answers

“Origin null is not allowed by Access-Control-Allow-Origin” error for request made by application running from a file:// URL

I'm developing a page that pulls images from Flickr and Panoramio via jQuery's AJAX support. The Flickr side is working fine, but when I try to $.get(url, callback) from Panoramio, I see an error in Chrome's console: XMLHttpRequest cannot load…
Drew Noakes
  • 266,361
  • 143
  • 616
  • 705
428
votes
21 answers

How to read a local text file?

I’m trying to write a simple text file reader by creating a function that takes in the file’s path and converts each line of text into a char array, but it’s not working. function readTextFile() { var rawFile = new XMLHttpRequest(); …
Danny
  • 7,700
  • 16
  • 46
  • 72
369
votes
8 answers

How many concurrent AJAX (XmlHttpRequest) requests are allowed in popular browsers?

In Firefox 3, the answer is 6 per domain: as soon as a 7th XmlHttpRequest (on any tab) to the same domain is fired, it is queued until one of the other 6 finish. What are the numbers for the other major browsers? Also, are there ways around these…
Michael Gundlach
  • 94,522
  • 11
  • 34
  • 39
347
votes
18 answers

Origin is not allowed by Access-Control-Allow-Origin

I'm making an Ajax.request to a remote PHP server in a Sencha Touch 2 application (wrapped in PhoneGap). The response from the server is the following: XMLHttpRequest cannot load http://nqatalog.negroesquisso.pt/login.php. Origin…
Ricardo
  • 10,019
  • 8
  • 23
  • 37
304
votes
10 answers

Why am I getting an OPTIONS request instead of a GET request?

it does an OPTIONS request to that URL, and then the…
Paul Tarjan
  • 44,540
  • 54
  • 163
  • 207
292
votes
5 answers

Proper way to catch exception from JSON.parse

I’m using JSON.parse on a response that sometimes contains a 404 response. In the cases where it returns 404, is there a way to catch an exception and then execute some other code? data = JSON.parse(response, function (key, value) { var type; …
prostock
  • 8,587
  • 18
  • 66
  • 115
239
votes
10 answers

What is difference between Axios and Fetch?

I am calling the web service by using fetch but the same I can do with the help of axios. So now I am confused. Should I go for either axios or fetch?
Gorakh Nath
  • 6,352
  • 11
  • 33
  • 58
224
votes
10 answers

Why does my http://localhost CORS origin not work?

I am stuck with this CORS problem, even though I set the server (nginx/node.js) with the appropriate headers. I can see in Chrome Network pane -> Response Headers: Access-Control-Allow-Origin:http://localhost which should do the trick. Here's the…
whadar
  • 3,209
  • 4
  • 17
  • 19
209
votes
10 answers

XMLHttpRequest Origin null is not allowed Access-Control-Allow-Origin for file:/// to file:/// (Serverless)

I'm trying to create a website that can be downloaded and run locally by launching its index file. All the files are local, no resources are used online. When I try to use the AJAXSLT plugin for jQuery to process an XML file with an XSL template (in…
Kevin Herrera
  • 2,921
  • 3
  • 17
  • 13
209
votes
4 answers

How to get the response of XMLHttpRequest?

I'd like to know how to use XMLHttpRequest to load the content of a remote URL and have the HTML of the accessed site stored in a JS variable. Say, if I wanted to load and alert() the HTML of http://foo.com/bar.php, how would I do that?
Rohan
  • 3,195
  • 4
  • 18
  • 16
202
votes
6 answers

How do I promisify native XHR?

I want to use (native) promises in my frontend app to perform XHR request but without all the tomfoolery of a massive framework. I want my xhr to return a promise but this doesn't work (giving me: Uncaught TypeError: Promise resolver undefined is…
SomeKittens
  • 35,809
  • 19
  • 104
  • 135
194
votes
9 answers

Pure JavaScript Send POST Data Without a Form

Is there a way to send data using the POST method without a form and without refreshing the page using only pure JavaScript (not jQuery $.post())? Maybe httprequest or something else (just can't find it now)?
John
  • 6,080
  • 14
  • 58
  • 91
1
2 3
99 100