3

I'm having trouble with XDomainRequest onerror handler. When error occurs, I don't seem to be able to get the body of HTTP response (through xdr.responseText). Successful requests (200 OK) work fine.

Here is the request:

POST http://localhost/path HTTP/1.1
Accept: */*
Origin: http://localhost
Accept-Language: cs-CZ
Accept-Encoding: gzip, deflate
User-Agent: Mozilla/5.0 (compatible; MSIE 9.0; Windows NT 6.1; WOW64; Trident/5.0)
Host: localhost
Content-Length: 34
Connection: Keep-Alive
Pragma: no-cache

[Data]

And here is the response:

HTTP/1.1 400 Bad Request
Content-Length: 54
Content-Type: application/json; charset=utf-8
Server: Microsoft-IIS/7.5
Access-Control-Allow-Origin: *
Date: Mon, 30 Apr 2012 20:57:32 GMT

[Data]

Is it possible to obtain the response body?

sideshowbarker
  • 81,827
  • 26
  • 193
  • 197
Tom Pažourek
  • 9,582
  • 8
  • 66
  • 107

1 Answers1

4

Unfortunately it is not possible to get the response body on error. All you can know is that an error has occurred. See my related question here: Is it possible to trap CORS errors?

One possible workaround (if you have control over the server) is to always return HTTP 200, but include the error code in the body of the response.

Community
  • 1
  • 1
monsur
  • 45,581
  • 16
  • 101
  • 95
  • How is it possible that it works in Chrome (which is using XmlHttpRequest)? Is it only limitation of XDomainRequest or is Chrome implementation wrong? – Tom Pažourek Apr 30 '12 at 22:47
  • I unfortunately don't have access to a Windows box at the moment, but I can verify that Chrome does indeed return the body. Note that IE's XDomainRequest is a different object than Chrome's XmlHttpRequest (Level 2), so they don't have to agree on behavior. IE10 will support XmlHttpRequest and CORS. – monsur May 02 '12 at 17:12
  • Firefox works the same as Chrome. I can still get response even in case of error. – Tom Pažourek May 02 '12 at 20:02