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
229
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
  • 38,868
  • 19
  • 114
  • 143
215
votes
2 answers

jQuery posting valid json in request body

So according to the jQuery Ajax docs, it serializes data in the form of a query string when sending requests, but setting processData:false should allow me to send actual JSON in the body. Unfortunately I'm having a hard time determining first, if…
brad
  • 31,987
  • 28
  • 102
  • 155
210
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,941
  • 3
  • 19
  • 13
206
votes
8 answers

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

I have made a small xslt file to create an html output called weather.xsl with code as follows:
dudledok
  • 2,800
  • 5
  • 24
  • 36
197
votes
10 answers

HTML5 Pre-resize images before uploading

Here's a noodle scratcher. Bearing in mind we have HTML5 local storage and xhr v2 and what not. I was wondering if anyone could find a working example or even just give me a yes or no for this question: Is it possible to Pre-size an image using the…
Jimmyt1988
  • 20,466
  • 41
  • 133
  • 233
172
votes
15 answers

What does it mean when an HTTP request returns status code 0?

What does it mean when JavaScript network calls such as fetch or XMLHttpRequest, or any other type of HTTP network request, fail with an HTTP status code of 0? This doesn't seem to be a valid HTTP status code as other codes are three digits in HTTP…
mike nelson
  • 21,218
  • 14
  • 66
  • 75
157
votes
11 answers

Why am I seeing an "origin is not allowed by Access-Control-Allow-Origin" error here?

I am seeing the following error: Origin http://localhost:8080 is not allowed by Access-Control-Allow-Origin with this code: var http = new getXMLHttpRequestObject(); var url = "http://gdata.youtube.com/action/GetUploadToken"; var sendXML = '
Muhammad Usman
  • 10,426
  • 22
  • 72
  • 107
155
votes
10 answers

How to enable CORS in AngularJs

I have created a demo using JavaScript for Flickr photo search API. Now I am converting it to the AngularJs. I have searched on internet and found below configuration. Configuration: myApp.config(function($httpProvider) { …
ankitr
  • 5,992
  • 7
  • 47
  • 66
154
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
  • 57,710
  • 92
  • 283
  • 453
147
votes
3 answers

How does JavaScript handle AJAX responses in the background?

Since JavaScript runs in a single thread, after an AJAX request is made, what actually happens in the background? I would like to get a deeper insight into this, can anyone shed some light?
aziz punjani
  • 25,586
  • 9
  • 47
  • 56
147
votes
6 answers

What is the difference between XMLHttpRequest, jQuery.ajax, jQuery.post, jQuery.get

How can I find out which method is best for a situation? Can anybody provide some examples to know the difference in terms of functionality and performance?
Rodrigues
  • 1,573
  • 2
  • 10
  • 4
144
votes
4 answers

Allow Google Chrome to use XMLHttpRequest to load a URL from a local file

When trying to do a HTTP request using XMLHttpRequest from a local file, it basically fails due to Access-Control-Allow-Origin violation. However, I'm using the local web page myself, so I was wondering if there is any way to make Google Chrome…
pimvdb
  • 151,816
  • 78
  • 307
  • 352
139
votes
11 answers

No 'Access-Control-Allow-Origin' header is present on the requested resource. Origin '...' is therefore not allowed access

I'm using .htaccess to rewrite urls and I used html base tag in order to make it work. Now, when I try to make an ajax request I get the following error: XMLHttpRequest cannot load http://www.example.com/login.php. No 'Access-Control-Allow-Origin'…
Th3lmuu90
  • 1,717
  • 2
  • 15
  • 18
138
votes
19 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
  • 1,381
  • 2
  • 9
  • 4
137
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
  • 5,073
  • 6
  • 39
  • 47