San*_*ang 110 ajax jquery internet-explorer cross-domain
我需要使用foursquare API来搜索场地.当然它是跨域的.
它在Firefox中没有任何问题,但在Internet Explorer中(7,8,9我测试过).
我的javascript代码如下:
searchVenues: function(searchQuery) {
$.ajax({
url: 'https://api.foursquare.com/v2/venues/search',
data: {
sw: bound_south_west,
ne: bound_north_east,
query: searchQuery.query,
oauth_token: FSQ_OAUTH_TOKEN,
limit: 25,
intent: 'browse',
v: 20120206
},
cache: false,
dataType: 'json',
success: function(data) {
displayResults(data, searchQuery.query);
},
error: function(xhr, status, errorThrown) {
console.log(errorThrown+'\n'+status+'\n'+xhr.statusText);
}
});
}
Run Code Online (Sandbox Code Playgroud)
在Firefox中,它可以完美地显示接收的数据.在Internet Explorer中,它在控制台上登录:
No Transport
Error
Error
Run Code Online (Sandbox Code Playgroud)
我该怎么办?
Mag*_*ico 264
我在Windows Mobile 7上测试了这个.
花了很多时间来理解之后,我终于找到了这个:
http://bugs.jquery.com/ticket/10660
解决方案很简单,只需设置:
$.support.cors = true;
Run Code Online (Sandbox Code Playgroud)
和Ajax跨域请求将工作!
小智 13
jQuery.support.cors = true;
$.ajax({
crossDomain: true,
url: "",
type: "POST",
dataType: "xml",
data: soapMessage,
});
Run Code Online (Sandbox Code Playgroud)
您需要将跨域值设为true
小智 7
这个问题一直困扰着我.作为一种解决方法,我使用位于同一站点上的代理脚本.这样的脚本只是执行服务器到服务器的非ajax HTTP请求(想想curl和WinHttp.WinHttpRequest)并将状态和数据传递回调用者.它工作,但显然效率不高,因为它必须执行两个HTTP请求.
在我的例子中,解决方案是上述所有内容的组合加上'Access-Control-Allow-Origin'标题.
$.support.cors = true; // this must precede $.ajax({}) configuration
$.ajax({
crossDomain: true, // added in jQuery 1.5
headers: {
'Access-Control-Allow-Origin': '*'
},
...
});
Run Code Online (Sandbox Code Playgroud)
应答这些呼叫的Web服务也会响应"Access-Control-Allow-Origin:*"标头.
尝试此解决方案:
或者,在包含jquery之后,只需将此代码放入HTML中.
<!--[if lte IE 9]>
<script type='text/javascript' src='//cdnjs.cloudflare.com/ajax/libs/jquery-ajaxtransport-xdomainrequest/1.0.3/jquery.xdomainrequest.min.js'></script>
<![endif]-->
Run Code Online (Sandbox Code Playgroud)
归档时间: |
|
查看次数: |
88303 次 |
最近记录: |