我想在纯javascript ajax中捕获跨域响应,而不是使用jquery或其他外部库。
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();
我的回应
XMLHttpRequest cannot load 'http://xxxxx/layer.php?action=12&user_full_name=xyz&user_compa…e=xyz&callback=callRes'. No 'Access-Control-Allow-Origin' header is present on the requested resource. Origin 'http://test.test.com' is therefore not allowed access.
Uncaught NetworkError: Failed to execute 'send' on 'XMLHttpRequest': Failed to load 'http://xxxxx/layer.php?action=12&user_full_name=xyz&user_compa…e=xyz&callback=callRes'.
花了很多时间和谷歌搜索,但没有找到任何解决方案
建议是最受欢迎的,但不是脚本方法只有纯javascript ajax