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
100
votes
11 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,696
  • 2
  • 16
  • 20
95
votes
5 answers

A CORS POST request works from plain JavaScript, but why not with jQuery?

I'm trying to make a Cross Origin post request, and I got it working in plain JavaScript like this: var request = new XMLHttpRequest(); var params = "action=something"; request.open('POST', url, true); request.onreadystatechange = function() {if…
Magmatic
  • 1,754
  • 3
  • 19
  • 34
95
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,111
  • 2
  • 9
  • 7
93
votes
5 answers

How do I know if jQuery has an Ajax request pending?

I'm having some problems with a jQuery control we made. Suppose you have a dropdownlist that allows you to enter the ID of the item you're looking for, and when you press ENTER or lose focus in a textbox it validates via jQuery that the ID you…
sabanito
  • 1,501
  • 1
  • 12
  • 17
91
votes
3 answers

POST data in JSON format

I have some data that I need to convert to JSON format and then POST it with a JavaScript function.
Randy Smith
89
votes
8 answers

Persistent Service Worker in Chrome Extension

I need to define my Service Worker as persistent in my Chrome extension because I'm using the webRequest API to intercept some data passed in a form for a specific request, but I don't know how I can do that. I've tried everything, but my Service…
85
votes
7 answers

Timeout feature in the axios library is not working

I have set axios.defaults.timeout = 1000; I stopped the server that provides me with the APIs. But it takes more than 1s to timeout after sending a request. This is how my request looks: import axios from 'axios'; axios.defaults.timeout =…
shet_tayyy
  • 5,366
  • 11
  • 44
  • 82
85
votes
9 answers

How do I get the HTTP status code with jQuery?

I want to check if a page returns the status code 401. Is this possible? Here is my try, but it only returns 0. $.ajax({ url: "http://my-ip/test/test.php", data: {}, complete: function(xhr, statusText){ alert(xhr.status); …
horgen
  • 2,183
  • 10
  • 30
  • 34
85
votes
11 answers

How to check if the request is an AJAX request with PHP

I would like to check server-side if a request to my php page is an ajax request or not. I saw two ways to do this: First way: sending a GET parameter in the request which tells the page that this is an AJAX request…
BackSlash
  • 21,927
  • 22
  • 96
  • 136
83
votes
14 answers

How secure is a HTTP POST?

Is a POST secure enough to send login credentials over? Or is an SSL connection a must?
Matt
  • 5,249
  • 12
  • 40
  • 45
80
votes
5 answers

How to Detect Cross Origin (CORS) Error vs. Other Types of Errors for XMLHttpRequest() in Javascript

I'm trying to detect when an XMLHttpRequest() fails due to a Cross Origin Error as opposed to a bad request. For example: ajaxObj=new XMLHttpRequest() ajaxObj.open("GET", url, true); ajaxObj.send(null); Consider 4 cases for url: Case…
user2871305
  • 1,052
  • 1
  • 8
  • 7
77
votes
7 answers

Web Scraping in a Google Chrome Extension (JavaScript + Chrome APIs)

What are the best options for performing Web Scraping of a not currently open tab from within a Google Chrome Extension with JavaScript and whatever more technologies are available. Other JavaScript-libraries are also accepted. The important thing…
74
votes
5 answers

In JavaScript how do I/should I use async/await with XMLHttpRequest?

Full disclosure: I'd qualify myself as having intermediate JavaScript knowledge. So this is slightly above my experience level at this time. I've got a Google Chrome Extension that does an AJAX request for a local file:/// as soon as a page loads.…
jkupczak
  • 2,891
  • 8
  • 33
  • 55
74
votes
1 answer

How do I send an HTTP request from a Chrome extension?

I'm working on a chrome extension that sends a HTTP request. How do I send it to www.example.com with the parameter par with value 0? https://www.example.com?par=0 (the server reads the parameter par and does something) I found this article,…
74
votes
2 answers

$http doesn't send cookie in Requests

We are working on a RESTful Webservice with AngularJS and Java Servlets. When the user logs in, our backend sends a "Set-Cookie" header to the frontend. In Angular we access the header via $cookies (ngCookie - module) and set it. Now that the user…
Tim Daubenschütz
  • 2,053
  • 6
  • 23
  • 39