获取错误消息" XMLHttpRequest无法加载http://api.vateud.net/notams/warj.json。 No' Access-Control-Allow-Origin'标头出现在请求的资源上。起源' http://run.plnkr.co'因此不允许访问"
这里是plunker链接:http://plnkr.co/edit/Kt340aO4WTYPb5sjCUDj?p=preview
<!DOCTYPE html>
<html>
<head>
<script src="http://code.jquery.com/jquery-latest.js"></script>
</head>
<body>
<div class="div"></div>
<script>
$.ajax({
type: "GET",
url: "http://api.vateud.net/notams/warj.json",
processData: true,
data: {},
dataType: "json",
error: function(){ alert("Error"); },
success: function (data) {
$.each(data, function(i,item){
$("<p>").html(item.raw).appendTo(".div");
});
}
});
</script>
</body>
</html>
答案 0 :(得分:1)
错误是由于CORS,即交叉原始请求,您收到错误:
XMLHttpRequest cannot load http://api.vateud.net/notams/warj.json. No 'Access-Control-Allow-Origin' header is present on the requested resource. Origin 'null' is therefore not allowed access.
在此处了解有关CORS的更多信息"No 'Access-Control-Allow-Origin' header is present on the requested resource" ..
还要在您的系统上运行下面的代码示例,您将在控制台中看到任何其他错误。
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div class="div"></div>
<script>
$.ajax({
type: "GET",
url: "http://api.vateud.net/notams/warj.json",
processData: true,
data: {},
dataType: "json",
error: function(e){
console.log(e);
},
success: function (data) {
$.each(data, function(i,item){
$(".div").append('<p>'+item.raw+'</p>');
});
}
});
</script>