简体   繁体   English

使用JavaScript从JSON网址解析数据

[英]parse data from JSON url using javascript

I want to make an ajax request and parse the JSON response. 我想提出ajax请求并解析JSON响应。 I have tried to do so with this code but with no luck 我试图用这段代码这样做,但是没有运气

var url = 'http://api.steampowered.com/ISteamUser \
          /ResolveVanityURL/v0001/?key=private_key&vanityurl=zacktm'
$.getJSON(url, function(data) {
    alert(data);
});

The response that I get is: 我得到的答复是:

{
"response": {
    "steamid": "76561198181548891",
    "success": 1
  }
}

the output I want: 76561198181548891 我想要的输出: 76561198181548891

A temporary solution for development would be to use crossorigin.me 开发的临时解决方案是使用crossorigin.me

Code would be: 代码为:

var url = 'http://crossorigin.me/http://api.steampowered.com/ISteamUser/ResolveVanityURL/v0001/?key=0E385DC97C8423D8DD9CD11944187F38&vanityurl=zacktm'
$.getJSON(url, function(data) {
    alert(data.response.steamid);
});

I see CORS is enabled in your API. 我看到您的API中启用了CORS。

A temporary solution would be to disable the CORS filter on your browser, but to get a permanent solution you will need to add the CORS headers on your backend REST API. 临时解决方案是在浏览器上禁用CORS过滤器,但是要获得永久解决方案,则需要在后端REST API上添加CORS标头。

Once that is done you can try the following: 完成后,您可以尝试以下操作:

$.get('http://api.steampowered.com/ISteamUser/ResolveVanityURL/v0001/?key=0E385DC97C8423D8DD9CD11944187F38&vanityurl=zacktm', function(data) {
   alert(data.response.steamid);   
});

data.response.steamid will give you the steamid data.response.steamid将为您提供帮助

1. 1。

var url = 'https://itunes.apple.com/in/rss/topalbums/limit=10/json';

2. 2。

    $.getJSON(url, function(data) {

      alert(data.feed.entry[0]["im:name"].label);
    });
var url = 'http://api.steampowered.com/ISteamUser \
          /ResolveVanityURL/v0001/?key=private_key&vanityurl=zacktm';
$.get(url, function(data){
    var reply = $.parseJSON(data);
    // Or plain JavaScript
    // var reply= JSON.parse(data);
    alert(reply.reponse.steamid);
});

Note that you cannot use XmlHttpRequest to external servers with localhost if server allow-origin: localhost is not set 请注意,如果未设置server allow-origin:localhost,则无法将XmlHttpRequest用于具有localhost的外部服务器

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM