【发布时间】:2014-09-23 11:19:53
【问题描述】:
我想在不使用 jquery 或其他外部库的纯 javascript ajax 中捕获跨域响应。
var xmlhttp, response;
try {
xmlhttp = new XMLHttpRequest();
} catch(e) {
try {
xmlhttp = new ActiveXObject("Msxml2.XMLHTTP");
} catch (e2) {
try {
xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");
} catch (e) {}
}
}
xmlhttp.onreadystatechange = function() {
if (xmlhttp.readyState==4) {
if(callback != undefined) {
console.log(xmlhttp.responseText);
}
}
}
url = "http://xxxx.com/layer.php?callback=callRes";
xmlhttp.open("GET", url, false);
xmlhttp.setRequestHeader("Content-Type",
"application/json,application/javascript,text/javascript");
xmlhttp.send();
花了很多时间和谷歌搜索,但没有找到任何解决方案
建议是最受欢迎的,但不是脚本方法,只有纯 javascript ajax
【问题讨论】:
-
应该不可能。同源策略只能通过使用一些浏览器插件来避免。为什么不是 JSONP?
-
它会有是JSONP。
-
正如我在问题中提到的,我想使用纯 javascript ajax 而不是脚本方法
-
响应来自哪里?您有权调整标题吗?如果是这样,请使用 CORS 并调整标题以允许您的前端代码访问。如果您请求的资源在 S3 上,这甚至可以在 S3 上完成。
-
你可以访问这个文件xxxx.com/layer.php 吗?
标签: javascript ajax