I would like to show that alert(json); shows undefined because it is running before receiving a response.
Remember that "ajax" is "asynchronous", so a warning will be executed before your json value gets the opportunity to assign a value.
Also, even if that worked, your json variable would just refer to the XMLHttpRequest object that was created. If you want to access the data itself, you must do this in a callback.
var json; $.ajax({ url : 'http://www.example.com/experimental/service.php', type : 'jsonp', success : function(data) { alert('success'); json = data; alert(json); } });
EDIT:
There is also an error: callback, which you can assign. A callback will be executed if the server returns an error code. I assume this is what you intended to replicate with try / catch.
user113716
source share