0

This is how I set the xmlhttp:

xmlhttp = new XMLHttpRequest();

I followed the instructions in this post that explains how to send a POST request.

xmlhttp.open("POST", url, false);//false means synchronous
xmlhttp.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
xmlhttp.withCredentials = true;
xmlhttp.send(postData);// This line throws Network Error exception.

Notes:

This URL is a .asmx web service.

This web service works fine, when I try to get data from it using the browser.

When I try running this code in the same domain where the web service is located, the code runs fine, and returns the data as I expect.

Most of my code, is based on async calls in which the same problem occurs, this example is sync only for simplicity.

My problem:

When I try to run this code from localhost, the line that I mentioned throws an exception. I checked the actual response that returns from the web service, and turns out that it contains the data it should.

HTTP/1.1 200 OK
Cache-Control: private, max-age=0
Server: Microsoft-IIS/7.5
X-AspNet-Version: 4.0.30319
Persistent-Auth: true
X-Powered-By: ASP.NET
Access-Control-Allow-Origin: *
Access-Control-Allow-Methods: GET, POST
Access-Control-Allow-Headers: Origin,cache-control,content-type,man,messagetype,soapaction
Date: Mon, 09 Dec 2013 18:20:03 GMT
Content-Length: 563

The actual data exists here in the response....

Edit:

When I use async call, the request is ended with status 0 but it still has the data.

Community
  • 1
  • 1
Yosi Dahari
  • 5,936
  • 5
  • 20
  • 40
  • Read the question more carefully, Most of my use is async.. where the same problem exists, I have added to the question where the same exception is thrown. – Yosi Dahari Dec 09 '13 at 18:38
  • OK, I had derived from your question's title that you have a problem with the synchronous request. Please don't make "*examples sync only for simplicity*" - synchronous requests are despised and have their own problems, async should be the default. You should highlight (focus) your question on the fact that it does work differently on your localhost. – Bergi Dec 09 '13 at 18:44
  • When using `withCredentials`, you need to explicitly state the `localhost` domain in the `Access-Control-Allow-Origin`. – Bergi Dec 09 '13 at 18:53
  • Explicitly stated the domain - `Access-Control-Allow-Origin: localhost` and it did not difference..(btw - this makes the meaning of `*` confusing..) – Yosi Dahari Dec 09 '13 at 18:57
  • It would need to be `http://localhost` of course… And yes, "*The string "\*" cannot be used for a resource that supports credentials*" - you don't want to allow credentialed requests from everywhere for security reasons. – Bergi Dec 09 '13 at 20:44

0 Answers0