user1460217
user1460217

Reputation:

$.getJSON on chrome extension fails to work properly

I'm creating a chrome extension, and want to be able to get a profile picture from a username. Unfortunately, there isn't an api for the website and there is no correlation between the profile URL and the profile name. I figured I could hijack the search ajax and use it to achieve my goal. Unfortunately, it doesn't seem to be working. I've added the permissions, and it isn't running in a content script, but it's still not working.

$.getJSON("http://www.website.com/user/search",{user:name},function(a){
    alert(1);
    alert(a.data);
});

It's failing silently, as nothing is happing. Yet, I can't figure out why. The json I should be receiving from the call (browsed to the site manually) is: {"error":false,"action":null,"one":true,"data":143217} Unfortunately it's not working. JSONP isn't an option, as the site obviously has no need of supporting it, so help me please. I don't see what I'm doing wrong.

EDIT: I see the problem. When I try to do the request I'm being redirected to user/search ( no ?user) which fails.

Upvotes: 0

Views: 459

Answers (2)

Lance
Lance

Reputation: 1897

The site you're trying to reach requires authentication before allowing a search to occur. You won't get a response from your ajax request, because the server is looking for a session with login credentials to allow the search to happen. Because your ajax is not authenticated, and does not have the session established, your request doesn't "fail", it is simply getting the server's 302 redirect response.

You would need to 'sign in' to the page you are trying to query in order to establish the session and any necessary variables before you would be able to proceed with your user search.

Upvotes: 1

Jim O'Brien
Jim O'Brien

Reputation: 2552

It could be entirely possible that the website's search API isn't returning the Content-type header as text/json, which is required for $.getJSON to function correctly.

Try simply using $.get and calling JSON.parse on the returned data.

Upvotes: 0

Related Questions