简体   繁体   中英

parse data from JSON url using javascript

I want to make an ajax request and parse the JSON response. 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

A temporary solution for development would be to use 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.

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.

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

1.

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

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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