简体   繁体   中英

Unable to get response from $.get

zip = 12345
url = "http://zip.elevenbasetwo.com/v2/US/"+zip; 
res = $.get(url)

When I run above code in the browser console then I can access res.responseJSON but When I run this code in my application.js then I can't access res.responseJSON I am getting undefine.

Does anybody know what's the issue or how can I access in my application.js ?

$.get is asynchronous . The response isn't immediatement available in the jqXHR object.

Pass a success callback to $.get :

$.get(url, function(data){
   // use data
});

The reason why it seems to work in the console is because you don't instantly open the object which is logged : the server answers before. That's not the case in your normal code : while the request is sent, the following line in your code is immediately executed, without waiting for the answer.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM