调用ajax返回函数外部?(Calling ajax returned outside the function? [duplicate])
这个问题在这里已有答案:
如何从异步调用返回响应? 32个答案 jQuery:ajax调用成功后返回数据[重复] 5个答案我试图调用在其函数之外返回的ajax,下面是我尝试过的代码但是没有用。
var names; $.ajax({ type: 'POST', url: 'ajax/get_upcoming.php' }).success(function (data) { names = data[1]; }); alert(names);This question already has an answer here:
How do I return the response from an asynchronous call? 35 answers jQuery: Return data after ajax call success [duplicate] 5 answersI am trying to call the ajax returned outside its function, below is the code I tried but didn't work.
var names; $.ajax({ type: 'POST', url: 'ajax/get_upcoming.php' }).success(function (data) { names = data[1]; }); alert(names);最满意答案
因为成功函数是回调函数,并在ajax请求获得响应时调用,所以改为尝试这个
var names; $.ajax({ type: 'POST', url: 'ajax/get_upcoming.php', success: performOperation }); function performOperation(data) { // write your code here names = data[1]; }Because success function is callback function and called when ajax request get the response , so instead try this
var names; $.ajax({ type: 'POST', url: 'ajax/get_upcoming.php', success: performOperation }); function performOperation(data) { // write your code here names = data[1]; }更多推荐
发布评论