我已经写了发送从客户端浏览器写在PHP我们的API跨域POST请求JavaScript方法:jQuery的AJAX POST请求丢弃的身体在IE9
$.ajax({
type: 'POST',
url: backend_url,
data: postArgs,
beforeSend: function(xhr, settings) {
xhr.setRequestHeader('Connection', 'close');
},
error: function(xhr, status, errorThrown) {
console.log('RECONTRIBUTION: Error on AJAX request! status=' + status + ', errorThrown=' + errorThrown + ', statusText=' + xhr.statusText);
$('#alert_message').showAlertMsg('error', 'Oops, we couldn\'t talk to our server! Please try again in a moment.');
},
success: function(data, status, response) {
//do things
}
});
在一切的伟大工程Firefox,Chrome和Safari,但在IE9中,我遇到了问题。第一个问题是我得到了一个“无传输”错误,我认为这是由于IE对跨域请求的限制造成的。我通过将jQuery.support.cors设置为true并包含此插件(通过在不同的SO线程中找到该插件)得到了请求:https://github.com/MoonScript/jQuery-ajaxTransport-XDomainRequest/blob/master/jQuery.XDomainRequest.js
现在请求已完成,但API返回错误,因为请求正文是失踪。从铬
工作请求(网址删节):
POST [backendurl] HTTP/1.1
Host: [backendurl]
Connection: keep-alive
Content-Length: 79
Origin: [frontendurl]
User-Agent: Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.1 (KHTML, like Gecko) Chrome/21.0.1180.83 Safari/537.1
Content-Type: application/x-www-form-urlencoded; charset=UTF-8
Accept: */*
Referer: [frontendurl]
Accept-Encoding: gzip,deflate,sdch
Accept-Language: en-US,en;q=0.8
Accept-Charset: ISO-8859-1,utf-8;q=0.7,*;q=0.3
DNT: 1
action=contributeSong&client=TUNEBOT&iTunes_id=17344380&user_id=&query_id=64600
而且从IE9非工作请求:
POST [backendurl] HTTP/1.1
Accept: */*
Origin: [frontendurl]
Accept-Language: en-US
Accept-Encoding: gzip, deflate
User-Agent: Mozilla/5.0 (compatible; MSIE 9.0; Windows NT 6.1; WOW64; Trident/5.0)
Host: [backendurl]
Content-Length: 0
Connection: Keep-Alive
Pragma: no-cache
怪异的部分是,我试图设置连接头到关闭因为我读到Keep-Alive有时会导致Internet Explorer出现问题......但请求仍然会发送Connection:Keep-Alive。有任何想法吗?我已经把头发撕了几天。好吧,所以在做了一些关于XDR对象的更多阅读后,我意识到我无法发送定制头文件,这解释了Keep-Alive问题。我看了一下插件,它没有发送任何数据到服务器(...),所以现在我已经请求APPEARS发送数据,但仍然得到相同的响应...
POST [backendurl] HTTP/1.1
Accept: */*
Origin: [frontendurl]
Accept-Language: en-US
Accept-Encoding: gzip, deflate
User-Agent: Mozilla/5.0 (compatible; MSIE 9.0; Windows NT 6.1; WOW64; Trident/5.0)
Host: [backendurl]
Content-Length: 79
Connection: Keep-Alive
Pragma: no-cache
action=contributeSong&client=TUNEBOT&iTunes_id=23148795&user_id=&query_id=64612
如下所示,添加cache:false
选项似乎不影响任何内容。是Connection:Keep-Alive
这里有什么让我难过的?
您能否介绍一下如何添加请求的数据?它看起来像你使用的插件传递数据...我使用$ postdata = file_get_contents(“php:// input”);以获得原始的POST数据,但它可以工作,但我希望能够像我在其他地方一样使用$ _POST – ashgromnies
您是否可以将您的代码作为此答案的一部分来帮助其他人? – ClearCloud8