2012-09-05 31 views
3

我已经写了发送从客户端浏览器写在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这里有什么让我难过的?

回答

1

修正了它!我下载的IE9 XDR插件在执行XDR请求时没有向服务器发送任何数据,因此我将数据添加到请求中,并根据需要修改了我的API以解析$ HTTP_RAW_POST_DATA变量。真是太痛苦了,我希望IE10和它的发展一样具有革命性......

+2

您能否介绍一下如何添加请求的数据?它看起来像你使用的插件传递数据...我使用$ postdata = file_get_contents(“php:// input”);以获得原始的POST数据,但它可以工作,但我希望能够像我在其他地方一样使用$ _POST – ashgromnies

+0

您是否可以将您的代码作为此答案的一部分来帮助其他人? – ClearCloud8

1

试试这个:只要你打电话给你的$().ready(function() { - 确保设置此:

$.ajaxSetup({ cache: false }); 

这避免了通过IE试图成为智能引起了问题。

+0

根据Fiddler的说法,似乎没有什么帮助 - 没有改变传出的请求。任何其他想法?它可能与我必须下载的CORS插件有关,以便首先完成请求? 我也很怀疑Connection头没有改变......但是我不完全知道如何弄清楚我的beforeSend更改在哪里被丢弃...... – dmajoraddnine