fetching a url using $.getJSON and parsing it correctly

Posted by sofia on Stack Overflow See other posts from Stack Overflow or by sofia
Published on 2010-04-17T05:25:59Z Indexed on 2010/04/17 5:33 UTC
Read the original article Hit count: 400

Filed under:
|
|

This is probably really stupid but i can't find the problem with my code. It fetches a url that returns json and the function is then supposed to return a string:

function getit() {
var ws_url = 'example.com/test.js';
var user = false;
$.getJSON(ws_url, function(data) {
    alert('user '+data.user);//shows john
    user = data.user || false;
    }
);
return user;//should return john but returns false

}

test.js will have something like this:

{"id":"12","username":"ses","user":"john","error":""}

or like this:

{"error":"123"}

I also tried if(data.user){}else{} but it didn't work either..

So what am i missing?

Thanks :)

© Stack Overflow or respective owner

Related posts about jQuery

Related posts about JavaScript