Javascript 无法获取json数据
我正在尝试从这个链接读取数据 但它没有显示任何数据。有什么问题吗?? 这是我的主页。该页面位于wamp服务器的www文件夹中Javascript 无法获取json数据,javascript,json,Javascript,Json,我正在尝试从这个链接读取数据 但它没有显示任何数据。有什么问题吗?? 这是我的主页。该页面位于wamp服务器的www文件夹中 <!DOCTYPE html> <html> <head> <!-- Latest compiled JavaScript --> <script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/js/bootstrap.min.js">
<!DOCTYPE html>
<html>
<head>
<!-- Latest compiled JavaScript -->
<script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/js/bootstrap.min.js"></script>
<!-- jQuery library -->
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.2.0/jquery.min.js"></script>
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css">
<style>
div.message {
background-color: black;
color: white;
margin: 20px 0 20px 0;
padding: 20px;
}
</style>
</head>
<body>
<script>
$(document).ready(function() {
$("#click").on("click", function() {
$.getJSON("https://www.freecodecamp.com/json/cats.json", function(json) {
var html = "";
// Only change code below this line.
json.forEach(function(val){
var keys=Object.keys(val);
html+="<div class='cat'>";
keys.forEach(function(key){
html += "<strong>"+key+"</strong>: " + val[key] + "<br>";
});
html += "</div><br>";
});
// Only change code above this line.
$(".message").html(html);
});
});
});
</script>
<div id="tops">
<div class="well">
<input type="text" id="user_id">
<button type="button" class="btn btn-default" id="click">See</button>
</div>
</div>
<div id="here" class="well message">
</div>
<!--<script type="text/javascript" src="cros_web.js"></script>-->
</body>
</html>
分区信息{
背景色:黑色;
颜色:白色;
保证金:20px 0 20px 0;
填充:20px;
}
$(文档).ready(函数(){
$(“#单击”)。在(“单击”,函数(){
$.getJSON(“https://www.freecodecamp.com/json/cats.json,函数(json){
var html=“”;
//仅更改此行下方的代码。
forEach(函数(val){
var keys=Object.keys(val);
html+=“”;
键。forEach(功能(键){
html+=“”+key+”:“+val[key]+”
”;
});
html+=“
”;
});
//仅更改此行上方的代码。
$(“.message”).html(html);
});
});
});
看见
远程服务器似乎不支持CORS,因此从客户端的角度来看,您没有什么可以做的。如果您希望能够从浏览器中调用,可以联系站点管理员为您的域启用CORS。有人能解释一下吗
您似乎知道这是CORS问题-您甚至标记了post CORS,并拥有CORS在标题中-因此,为了解释,该站点不允许跨源资源共享-这就是为什么OneError会触发(因为请求有一个错误,并且status==0肯定是CORS错误的标志)@chris-“简单请求”只会避免“CORS飞行前”,而不是“CORS”-问题中的请求是一个简单的请求-实际上,请注意,由于内容类型标题,这不是一个简单的请求-但是,删除不会“围绕CORS工作”的请求-没有围绕CORS工作的事情。使用JSON从站点获取数据的简单方法是什么help@PHPglue - 1)如果客户机没有XHR2,那么他们使用的是非常旧的浏览器-2)OP如何处理代码中的异步性没有问题,如图所示