我在 Opera 中遇到了跨域 ajax 的大麻烦。我正在尝试使用EasyXDM,但 jQuery 在 Opera 中崩溃并出现错误:
Uncaught exception: TypeError: 'contents[ type ].test' is not a function
Error thrown at line 7847, column 3 in ajaxHandleResponses(s, jqXHR, responses) in https://ajax.googleapis.com/ajax/libs/jquery/1.7.2/jquery.js:
if ( contents[ type ] && contents[ type ].test( ct ) )
called from line 7479, column 3 in done(status, nativeStatusText, responses, headers) in https://ajax.googleapis.com/ajax/libs/jquery/1.7.2/jquery.js:
var isSuccess,
called from line 8324, column 7 in <anonymous function: callback>(_, isAbort) in https://ajax.googleapis.com/ajax/libs/jquery/1.7.2/jquery.js:
complete( status, statusText, responses, responseHeaders );
Uncaught exception: TypeError: 'contents[ type ].test' is not a function
这说明了 jQuery 核心中的以下部分:
// Remove auto dataType and get content-type in the process
while( dataTypes[ 0 ] === "*" ) {
dataTypes.shift();
if ( ct === undefined ) {
ct = s.mimeType || jqXHR.getResponseHeader( "content-type" );
}
}
// Check if we're dealing with a known content-type
if ( ct ) {
for ( type in contents ) {
if ( contents[ type ] && contents[ type ].test( ct ) ) {
dataTypes.unshift( type );
break;
}
}
}
标题是:
POST /api/api.do HTTP/1.1
User-Agent: Opera/9.80 (Windows NT 5.1; U; ru) Presto/2.10.229 Version/11.62
Host: DOMAIN-A.com
Accept-Language: ru-RU,ru;q=0.9,en;q=0.8
Accept-Encoding: gzip, deflate
Referer: http://DOMAIN-A.com/api/api.do?cors=1&xdm_e=http%3A%2F%2Flocalhost& xdm_c=default1063&xdm_p=1
Cookie: BSN=e63bcbd283267d6499e1df2313ad4fd7
Connection: Keep-Alive
Content-Length: 244
Content-Type: application/x-www-form-urlencoded; charset=UTF-8
Accept: */*
X-Requested-With: XMLHttpRequest
Content-Transfer-Encoding: binary
HTTP/1.1 200 OK
Server: nginx/0.7.65
Date: Thu, 04 Oct 2012 19:47:57 GMT
Content-Type: text/plain; charset=utf-8
Connection: keep-alive
Vary: Accept-Encoding
X-Powered-By: PHP/5.2.17
P3P: CP="IDC DSP COR ADM DEVi TAIi PSA PSD IVAi IVDi CONi HIS OUR IND CNT"
Access-Control-Allow-Origin: http://DOMAIN-A.com
Access-Control-Allow-Methods: POST, GET, OPTIONS
Access-Control-Max-Age: 1728000
Access-Control-Allow-Headers: Content-Type
Access-Control-Allow-Credentials: true
Expires: Thu, 19 Nov 1981 08:52:00 GMT
Cache-Control: no-store, no-cache, must-revalidate, post-check=0, pre-check=0
Pragma: no-cache
Content-Encoding: gzip
我按预期从 API.DO 得到响应,但我无法使用它,因为 javascript 崩溃了!
我正在使用 Opera 11。
PS:我尝试更改响应的内容类型,但没有效果...