HTTP Get Request in JQuery to Last.fm
Asked Answered
C

4

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

Changeover answered 2/7, 2010 at 20:3 Comment(0)
W
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

Whorled answered 3/7, 2010 at 6:24 Comment(0)
P
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.

Preoccupancy answered 2/7, 2010 at 20:5 Comment(3)
How do I perform this HTTP Get request then? Here is some more information from last.fm's documentation last.fm/api/webauthChangeover
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.Preoccupancy
There is no reason to proxy as $getJSON() was designed specifically for applications like this.Swinford
S
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

Swinford answered 2/7, 2010 at 20:9 Comment(7)
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.Preoccupancy
This is patently false. Read up on $getJSON().Swinford
I stand corrected. As long as you set the flag to indicate JSONP, you should be able to access a different origin.Preoccupancy
Is this correct? $.getJSON("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.Changeover
$.getJSON("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.Changeover
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.Swinford
you need a callback in the url look at the example I postedBoride
B
1

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

Boride answered 2/7, 2010 at 20:10 Comment(2)
$.getJSON("last.fm/api/auth/…?", 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] <!DOCTYPE html>\n"Changeover
It seems this is because I requested a JSON response but the return was in HTMLChangeover
W
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

Whorled answered 3/7, 2010 at 6:24 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.