Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 2 of 2
  1. #1
    New to the CF scene
    Join Date
    Jun 2013
    Posts
    1
    Thanks
    0
    Thanked 0 Times in 0 Posts

    How to retrieve the JSON data from url

    Hi,

    I have a currency quotes link, which outputs JSON format.
    http://api.apirates.com/api/update

    I tried to retrieve properties in that JSON format using $.getJSON, $.get in Jquery.
    But, i am unable to retrieve properties.

    Can you please tell the process how to get properties individually from this URL using Jquery or Javascript.

    Thank you,
    SNV

  • #2
    Regular Coder
    Join Date
    Aug 2010
    Posts
    974
    Thanks
    19
    Thanked 212 Times in 210 Posts
    Code:
     
    <!DOCTYPE html>
    <html>
    <body>
    <script>
    req = new XMLHttpRequest;
    req.open("GET","http://api.apirates.com/api/update",false);
    req.send();
    res = JSON.parse(req.responseText);
    (function writeIt(obj){
       for(a in obj){ 
          if(typeof obj[a] == "object" ){
             //document.write(a,"<br>");
             document.body.appendChild(document.createTextNode(a))
             document.body.appendChild(document.createElement("BR"))
             writeIt(obj[a])
       //}
       //else document.write(a," = ",obj[a],"<br>");
          }else{
             document.body.appendChild(document.createTextNode(a+" = "+obj[a]))
             document.body.appendChild(document.createElement("BR"))
          }
       }
    })(res);
    </script>
    </body>
    </html>
    
    only works from same origin
    unless you include
    Access-Control-Allow-Origin header

    it might be better to
    use jsonp instead
    Last edited by DaveyErwin; 06-26-2013 at 10:21 PM.


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •