XDomainRequest - 出错时获取响应正文

时间:2012-04-30 21:02:21

标签: cors xdomainrequest

我遇到了XDomainRequest onerror处理程序的问题。当发生错误时,我似乎无法获得HTTP响应的主体(通过xdr.responseText)。成功请求(200 OK)正常工作。

以下是请求:

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]

以下是回复:

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]

是否有可能获得回复正文?

1 个答案:

答案 0 :(得分:4)

不幸的是,错误时无法获得响应正文。您只能知道发生了错误。请在此处查看我的相关问题:Is it possible to trap CORS errors?

一种可能的解决方法(如果您可以控制服务器)是始终返回HTTP 200,但在响应正文中包含错误代码。

相关问题