AJAX调用后从嵌套函数返回 [英] return from nested function after AJAX call

查看:129
本文介绍了AJAX调用后从嵌套函数返回的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有这种情况,我必须通过AJAX检查某些内容,然后返回结果。
或者简化:

 函数isValid(){
$ .ajax(URL,data,function(响应){
isValid =响应;
});
返回isValid;
}

但仍然无法购买。



我可以访问响应,但是无法在获取响应后使isValid返回。
例如我遇到的解决方案:

 函数isValid(){
函数checkOnServer(callback) {
$ .ajax(URL,数据,函数(响应){
回调(响应);
});
}
checkOnServer(function(response){
alert(response);
});
}

我遇到过遍布于Stackoverflow的问题,但问题在于:


  1. 我不想提醒。

  2. 我想从isValid()返回它。 li>

===========================

你的代码是明智的,但你在isValid()中使用你的回调函数,所以它不会提醒!,我也有点改变你的ajax函数低于

  function isValid(){
checkOnServer(function(response){
alert(response);
});

函数checkOnServer(callback){
$ .ajax({
url:URL,
data:data,
success:function(response) {
callback(response);
}
});

编辑

变量不能从异步方法返回?如果我们用ajax调用函数,调用函数在ajax响应之前立即运行,所以它只会用上面的方法返回undefined。



有两种方法我知道要从异步函数返回



调用ajax函数完成

 函数isValid(){
var data;
checkOnServer(function(response){
data = response;
});
返回数据;

函数checkOnServer(callback){
$ .ajax({
url:URL,
data:data,
async:false
)}。done(function(response){
callback(response);
});
}
console.log(isValid());

使用 responseText指定变量

 函数isValid(){
var data = checkOnServer();
返回数据;

函数checkOnServer(){
var data = $ .ajax({
url:URL,
data:data,
async:false / /必须设置async为false
});
返回data.responseText;
}
console.log(isValid());


I have this situation where I have to check something via AJAX and then return the result. Or simplified:

function isValid() {
    $.ajax(URL, data, function(response) {
        isValid = response;
    });
    return isValid;
}

but still can't make it.

I can access the reponse, but I can't make isValid return AFTER I get the reponse. For example the solution I meet everywhere:

function isValid() {
    function checkOnServer(callback) {
        $.ajax(URL, data, function(response) {
            callback(response);
        });
    }
    checkOnServer(function(response) {
        alert(response);
    });
}

I met this all over Stackoverflow, BUT the problem is:

  1. I don't want to alert this.
  2. I want to RETURN it from isValid().

===========================

EDIT: I forgot to mention that if you/me/we try to simply "return" from the "checkOnServer" - it will just return it to the success callback of the AJAX. But the goal here is to make "isValid" return the result ... :)

解决方案

your code is sensible ,but you use your callback function in isValid() so its doesn't alert !,also I've a little bit change your ajax function below

function isValid() { 
  checkOnServer(function(response) {
    alert(response);
  });
}
function checkOnServer(callback) {
    $.ajax({
         url : URL,
         data : data,
         success : function(response) {
           callback(response);
         }
     });
}

Edit

Variable is can't return from asynchronous method ?If we call function with ajax ,call function is run first immediately before ajax is response,so it will return undefined only with above method.

There is two method I've know to return from asynchronous function

Call after ajax function is done

 function isValid() {
  var data; 
  checkOnServer(function(response) {
    data = response;
  });
  return data;
}
function checkOnServer(callback) {
    $.ajax({
         url : URL,
         data : data,
         async : false             
     }).done(function(response){
        callback(response);
        });
}
console.log(isValid());

Assign variable with responseText

 function isValid() { 
 var data = checkOnServer();
 return data;
}
function checkOnServer() {
    var data = $.ajax({
                 url : URL,
                 data : data,
                 async : false //must set async to false
              });
    return data.responseText;
}
console.log(isValid());

这篇关于AJAX调用后从嵌套函数返回的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆