【发布时间】:2016-10-02 20:30:08
【问题描述】:
我有以下代码:
function ajax(callback, requestString){
console.log("basic ajax sending");
var xmlhttp;
// compatible with IE7+, Firefox, Chrome, Opera, Safari
xmlhttp = new XMLHttpRequest();
xmlhttp.onreadystatechange = function(){
if (xmlhttp.readyState == 4 && xmlhttp.status == 200){
callback(xmlhttp.responseText); //we got a response
}
}
xmlhttp.open("GET", requestString, true);
xmlhttp.send();
}
//Here: code to be able to use that function repeatedly.
如果在我的 node.js 服务器的域上使用它会很好,问题是我正在开发一个 API,并且请求必须跨域发送。这些请求(requestString)只是一个字符串,它的格式类似于:“http://example.com/r?a=a”+“&b=b”如果重要的话。我收到以下错误:无法加载 XMLHttpRequest。跨源请求仅支持协议方案:http、data、chrome、chrome-extension、https、chrome-extension-resource。我确实看到了 jQuery 和 jsonP 的解决方案,但我不想将 jQuery 推给我的客户,所以我必须找到一个好的解决方案...... 感谢您的宝贵时间!
【问题讨论】:
标签: javascript ajax node.js cross-domain