以下 Dojo 代码的加载回调返回 null。应该没有任何问题,因为 jQuery 的 ajax 工作正常。我在这里缺少什么?
Dojo 版本 - 不工作
dojo.xhrGet({
url:"http://localhost:11039/",
handleAs:"json",
load: function(data){
console.log(data); // Prints null
},
error: function(err){
console.log('Error: ' + err);
}
});
jQuery 版本 - 有效
$.ajax({
url:"http://localhost:11039/",
type: 'GET',
dataType: 'json',
success: function(res){
console.log(res) // Prints some JSON
},
error: function(err){
console.log('ERROR: ' + err);
}
});
查看 Firebugs 网络选项卡,我注意到 jQuery 版本实际上正在发送 GET 请求:
GET localhost:11039 200OK localhost:11039 62.8KB
而 Dojo 版本...“选项”?
OPTIONS localhost:11039 200OK localhost:11039 62.8KB
其他详细信息:
- 我在 Ffox、Chrome 和 Safari 中得到了相同的结果。
最佳答案
Dojo 发送 OPTIONS 请求通常意味着 dojo 认为这是一个跨域请求。
OPTIONS 请求正在检查 Access-Control-Allow-Origin header 以查看是否应执行该请求,即使它被认为是跨域的。
参见 https://developer.mozilla.org/En/HTTP_Access_Control了解更多详情
关于javascript - Dojo xhrGet 只返回 null,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6516232/