Hor*_*ter 2 javascript jquery cross-domain internet-explorer-9
我的Ajax跨域请求在IE 9中失败,并且"拒绝访问".我已经阅读了几篇关于这个主题的帖子,AFAIK应该可以使用.
async,jsonp并且crossdomain,cache是false.这些是我找到的先决条件.jQuery.support.cors 是真的Access-Control-Allow-Origin:*(SO)那么为什么这会因拒绝访问而失败?任何的想法?可能是因为我的代码是从"JavaScript"库中调用的,而不是<script></script>页面上的标签吗?
我错过了什么?
// The code is part of an object's method (prototype)
// code resides in a library "Mylib.js"
$.ajax({
type: 'GET',
url: url,
cache: false,
async: true,
crossdomain: true, // typo, crossDomain, see my answer below
datatype: "jsonp", // dataType
success: function (data, status) {
if (status == "success" && !Object.isNullOrUndefined(data)) { ... }
},
error: function (xhr, textStatus, errorThrown) {
// access denied
}
});
Run Code Online (Sandbox Code Playgroud)
- 编辑 - 根据Robotsushi的评论,进一步研究---
XDomainRequest在jQuery源代码中找不到(1.8.1)jQuery.support.cors = true),我最终会遇到"No Transport"异常.jQuery处理这个问题的方式,似乎是围绕下面的代码,但在我的特定情况下没有调用,不知道为什么?
//绑定脚本标记hack transport jQuery.ajaxTransport("script",function(s){
// This transport only deals with cross domain requests
if ( s.crossDomain ) {
Run Code Online (Sandbox Code Playgroud)2010年的类似情况:在跨域调用的IE中Jquery $ .ajax失败但是,这应该已经被后来的jQuery版本解决了.
好的,现在工作.我身边的几个错误:
crossDomain: true, dataType: "jsonp"错字 - 错过大写字母.考虑到所有事情,它的确有效.所以我的个人清单将是:
json尽可能使用(例如使用Chrome,FF或IE10).确保设置了响应标头:Access-Control-Allow-Origin:*jsonp,查询:async: true,jsonp和crossdomain: true,cache是false,jQuery.support.cors是true这是我发现的先决条件.jsonp响应是一个函数调用(函数中包含JSON),而不是 "普通的"JSON数据.| 归档时间: |
|
| 查看次数: |
19092 次 |
| 最近记录: |