7

I'm trying to make an HTTP Get request using JQuery, but I get an empty string as a response, so I figure I'm doing something wrong. I used the documentation from http://api.jquery.com/jQuery.get/ as a guide.

My code looks like this

$.get("http://www.last.fm/api/auth/?api_key=xxxkeyxxx", function(data){
     window.console.log(data);
  });

Edit: My code now looks like this

$.getJSON("http://www.last.fm/api/auth/?api_key=c99ddddddd69ace&format=json&callback=?", 
    function(data){
        window.console.log(data);
    });

But I'm getting a syntax error [Break on this error] \n

And it's located in http://www.last.fm/api/auth/?api_key=c99ddddddd69ace&format=json&callback=?

Latest edit: It seems this is because last.fm is responding with html not JSON, any ideas would be appreciated

Crothers
  • 177
  • 1
  • 2
  • 8

4 Answers4

3

Unless your script is being served from www.last.fm, then you will not be able to do this, due to the Same Origin Policy restrictions imposed by browsers.

You should investigate proxying the request through your server.

pkaeding
  • 36,513
  • 30
  • 103
  • 141
  • How do I perform this HTTP Get request then? Here is some more information from last.fm's documentation http://www.last.fm/api/webauth – Crothers Jul 02 '10 at 20:07
  • I updated my answer to include a work-around technique. Basically, the API described in the link you provided is meant to be accessed by server-side code (or code in a stand-alone application) rather than from javascript in the browser. – pkaeding Jul 02 '10 at 20:09
  • There is no reason to proxy as `$getJSON()` was designed specifically for applications like this. – HurnsMobile Jul 02 '10 at 20:13
1

pkaeding is partially correct - you wont be able to do this in the way you are attempting, but last.fm does offer a RESTful API with json.

Last.fm API - http://www.last.fm/api/rest

jQuery API - http://api.jquery.com

HurnsMobile
  • 4,341
  • 3
  • 27
  • 39
  • Even if you are getting the responses in JSON, you will not be able to access the service (directly) from javascript in the browser. You will still need to proxy the request. – pkaeding Jul 02 '10 at 20:10
  • This is patently false. Read up on `$getJSON()`. – HurnsMobile Jul 02 '10 at 20:11
  • I stand corrected. As long as you set the flag to indicate JSONP, you should be able to access a different origin. – pkaeding Jul 02 '10 at 20:16
  • Is this correct? $.getJSON("http://www.last.fm/api/auth/?api_key=xxxkeyxxx", function(data){ window.console.log(data); }); Can't seem to get my formatting right on replies. Sorry for it being hard to read. I'm getting an undefined as the return value. – Crothers Jul 02 '10 at 20:31
  • $.getJSON("http://www.last.fm/api/auth/?api_key=xxxxkeyxxxx&format=json", function(data){ window.console.log(data); }); Added in the JSON flag, but it doesn't seem to work still. – Crothers Jul 02 '10 at 20:38
  • You are going to want to open a new question for this as I am not familiar with the specifics of the last.fm API. – HurnsMobile Jul 02 '10 at 20:57
  • you need a callback in the url look at the example I posted – mcgrailm Jul 02 '10 at 20:58
1

you need to use jsonp method iin getting data cross domain here is an example and thread of someone doing so

Community
  • 1
  • 1
mcgrailm
  • 17,469
  • 22
  • 83
  • 129
  • $.getJSON("http://www.last.fm/api/auth/?api_key=c9946d11002aea09f23b14faac469ace&format=json&callback=?", function(data){ window.console.log(data); }); Code looks like this. I am getting the right return value, but I am getting an error in the return of "syntax error [Break on this error] \n" – Crothers Jul 02 '10 at 21:15
  • It seems this is because I requested a JSON response but the return was in HTML – Crothers Jul 02 '10 at 22:59
1

last.fm will respond with login page... check the docs ...

If the user is not logged in to Last.fm, they will be redirected to the login page before being asked to grant your web application permission to use their account. On this page they will see the name of your application, along with the application description and logo as supplied in Section 1.

copied from

http://www.last.fm/api/webauth

rbawaskar
  • 1,035
  • 2
  • 10
  • 25