views:

71

answers:

4

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

+3  A: 

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
How do I perform this HTTP Get request then?Here is some more information from last.fm's documentationhttp://www.last.fm/api/webauth
Crothers
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
There is no reason to proxy as `$getJSON()` was designed specifically for applications like this.
HurnsMobile
+1  A: 

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
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
This is patently false. Read up on `$getJSON()`.
HurnsMobile
I stand corrected. As long as you set the flag to indicate JSONP, you should be able to access a different origin.
pkaeding
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
$.getJSON("http://www.last.fm/api/auth/?api_key=xxxxkeyxxxx });Added in the JSON flag, but it doesn't seem to work still.
Crothers
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
you need a callback in the url look at the example I posted
mcgrailm
+1  A: 

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

mcgrailm
$.getJSON("http://www.last.fm/api/auth/?api_key=c9946d11002aea09f23b14faac469ace });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] <!DOCTYPE html>\n"
Crothers
It seems this is because I requested a JSON response but the return was in HTML
Crothers
A: 

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