从Javascript中的URL获取JSON数据
我正在尝试从Web服务器读取JSON数据,但丢失了。我没有收到任何错误,但我的代码也没有显示任何内容。这是样品从Javascript中的URL获取JSON数据,javascript,json,html,Javascript,Json,Html,我正在尝试从Web服务器读取JSON数据,但丢失了。我没有收到任何错误,但我的代码也没有显示任何内容。这是样品 <!DOCTYPE HTML> <html> <head> <script type="text/javascript" src="https://ajax.googleapis.com/ajax/libs/jquery/1.7/jquery.min.js"></script
<!DOCTYPE HTML>
<html>
<head>
<script type="text/javascript"
src="https://ajax.googleapis.com/ajax/libs/jquery/1.7/jquery.min.js"></script>
<script>
$(document).ready(function(){
$.getJSON('http://api.example.com/madata.php?zip=08854&key=36e25aa7518a6092&callback=?', function(json) { //this works. but doesn't display any alerts or data
//$.getJSON('http://api.geonames.org/citiesJSON?north=44.1&south=-9.9&east=-22.4&west=55.2&lang=de&username=demo&callback=?', function(json) { //this works. notice the callback
//$.getJSON('http://api.geonames.org/citiesJSON?north=44.1&south=-9.9&east=-22.4&west=55.2&lang=de&username=demo', function(json) { //this also works
alert("good");
alert(JSON.stringify(json));
console.log(JSON.stringify(json));
});
});
</script>
</head>
<body>
<div id = 'placeholder'></div>
</body>
</html>
如果我在这里做傻事,请告诉我。我对Javascript完全不熟悉,刚刚开始学习
$.get( "http://api.example.com/madata.php?zip=08854&key=36e25aa7518a6092&callback=?", function( data ) {
console.log(data.FCCID);
}, "json" );
这应该对你有帮助。您可以对返回的数据执行任何操作 如果您的代码没有在api.example.com上运行,那么您可能会被CORS问题绊倒-。@AdrianWragg是的,它没有在api.example.com上运行,这就是我使用JSONP发出请求的原因。至少我认为我做的是正确的。谢谢,但这和以前一样。查看控制台日志并粘贴错误。你能给我api的url来试用吗?那个服务器不支持JSONP或CORS,所以你需要一个服务器代理来使用JS中的api。@dandavis这是正确的。我确认它不支持JSONP。我现在必须找到另一种方法来获取数据。谢谢
$.get( "http://api.example.com/madata.php?zip=08854&key=36e25aa7518a6092&callback=?", function( data ) {
console.log(data.FCCID);
}, "json" );