2017-04-21 72 views
0

我想从使用ajax的URL获取响应文本。下面的代码工作正常,如果我设置async标志false但我得到的jQuery说如何异步获取响应文本?

jquery.min.js警告:4同步XMLHttpRequest的主线程上的,因为其所带来的影响到底是过时用户体验。

下面是代码:

function verifyUser() 
{ 
    var response = $.ajax({type: "GET", url: "/verify/4512h58", async: false}).responseText; 
    console.log(response); 
} 

,如果我设置async标志true像这样

var response = $.ajax({type: "GET", url: "/verify/112358", async: true}).responseText; 

我得到undefined作为输出。如何解决这个问题?

回答

2

这将使用一些所谓的承诺,所以它需要是这个样子......

var response = $.ajax({ 
    type: "GET", 
    url: "/verify/4512h58" 
}).done(function (response) { 
    console.log(response); 
}); 

See for more info

1

使用回调

var response = ""; 
$.ajax({type: "GET", url: "/verify/112358", async: true}) 
    .then(function(x){ 
     response = x.responseText 
    }); 

参见How do I return the response from an asynchronous call?

+0

用20K代表你应该知道标记这个问题的问题一个副本,而不是再次回答 – andrew

+0

@andrew - 我做到了,请参阅下面的问题评论。 – Igor

+0

所以......你只是想让它更难删除? –