jQuery 如何从json url获取数据
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/10100660/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to get data from json url
提问by
I would like to display data (country name and money value) from the https://raw.github.com/currencybot/open-exchange-rates/master/latest.jsonto my web page. Please suggest me a way to do so.
我想将https://raw.github.com/currencybot/open-exchange-rates/master/latest.json 中的数据(国家名称和货币价值)显示到我的网页上。请建议我这样做的方法。
回答by Sudhir Bastakoti
You can use JSONP for making such requests, but i think the url you are trying to access does not have JSONP feature. Since you want the exchange rates (i guess as per the url you are trying to use), you could use:
您可以使用 JSONP 来发出此类请求,但我认为您尝试访问的 url 没有 JSONP 功能。由于您想要汇率(我猜根据您尝试使用的网址),您可以使用:
$(document).ready(function(){
$.ajax({
url: 'http://openexchangerates.org/latest.json',
dataType: 'jsonp',
success: function(json) {
// Rates are in `json.rates`
// Base currency (USD) is `json.base`
// UNIX Timestamp when rates were collected is in `json.timestamp`
rates = json.rates;
base = json.base;
console.log(rates);
}
});
});
Ref: See Here
参考:见这里
Hope it helps
希望能帮助到你
回答by ErJab
This should work with jQuery:
这应该适用于jQuery:
$.ajax({
url: 'https://raw.github.com/currencybot/open-exchange-rates/master/latest.json',
dataType: 'jsonp',
success: function (data, textStatus, jqXHR) {
//the variable 'data' will have the JSON object
// In your example, the following will work:
alert(data.disclaimer);
error: function(jqXHR, textStatus, errorThrown) {
//Error handling code
alert('Oops there was an error');
}
}
});
回答by Nagaraju Badaeni
use jQuery.getJSON() function
使用 jQuery.getJSON() 函数
go through this tutorial http://api.jquery.com/jQuery.getJSON/