I use this helper function to receive JSON results for my requests:
function getData(url) {
$.get(url,
function(data) {
response =
It's an asynchronous operation, meaning that function(data) { ... }
runs later when the response from the server is available, long after you returned from getData()
. Instead, kick off whatever you need from that function, for example:
function getData(url, callback) {
$.get(url, callback, 'application/json');
}
Then when you're calling it, pass in a function or reference to a function that uses the response, like this:
getData("myPage.php", function(data) {
alert("The data returned was: " + data);
});
Use $.ajax
$.ajax({
url: 'http://www.example.com',
dataType: 'json',
success: function(data){
alert(data.Id);
}
});
try this
function getData(url) { var data; $.ajax({ async: false, //thats the trick url: 'http://www.example.com', dataType: 'json', success: function(response){ data = response; } }); return data; }