从 javascript 中的嵌套函数返回

发布于 2024-09-15 02:17:43 字数 564 浏览 4 评论 0原文

我正在尝试创建一个使用 jquery 的 ajaxfunction 从我的 ajax.php 文件中获取一些信息的函数。

code:

function ajaxIt(dataLine){
    $.ajax({
        type: "POST",
        url: "ajax.php",
        data: "ajax=true&"+dataLine,
        success: function(msg){
            console.log("[AjaxIt]: "+dataLine+" returned "+msg);
            return msg;
        }
    });
 }
 if(ajaxIt("action=loggedIn")=="1"){
       console.log("Logged In");
       loggedIn=true;
       initiate2();
 }

问题是我无法让 success 函数一路返回到 ajaxIt 函数。有人能告诉我如何做这样的事情吗?

谢谢。

I'm trying to make a function that uses jquery's ajaxfunction to get some info from my ajax.php file.

code:

function ajaxIt(dataLine){
    $.ajax({
        type: "POST",
        url: "ajax.php",
        data: "ajax=true&"+dataLine,
        success: function(msg){
            console.log("[AjaxIt]: "+dataLine+" returned "+msg);
            return msg;
        }
    });
 }
 if(ajaxIt("action=loggedIn")=="1"){
       console.log("Logged In");
       loggedIn=true;
       initiate2();
 }

The problem is that I can't get the success function to return all the way to the ajaxIt function. Could anyone shed some light onto how I could do something like that?

Thanks.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

蔚蓝源自深海 2024-09-22 02:17:43

您需要调用回调函数来以这种方式处理数据:

function ajaxIt(dataLine, cb){
    $.ajax({
        type: "POST",
        url: "ajax.php",
        data: "ajax=true&"+dataLine,
        success: function(msg){                
            if($.isFunction(cb))
               cb.apply(null, [msg]);
        }
    });
}

ajaxIt("action=loggedIn", function(data){
      if(data === "1"){
         console.log("Logged In");
         loggedIn=true;
         initiate2();
      }
});

You need to invoke a callback function to process data that way:

function ajaxIt(dataLine, cb){
    $.ajax({
        type: "POST",
        url: "ajax.php",
        data: "ajax=true&"+dataLine,
        success: function(msg){                
            if($.isFunction(cb))
               cb.apply(null, [msg]);
        }
    });
}

ajaxIt("action=loggedIn", function(data){
      if(data === "1"){
         console.log("Logged In");
         loggedIn=true;
         initiate2();
      }
});
朱染 2024-09-22 02:17:43

$.ajax 是异步的。这意味着它将立即返回,而不是等待 AJAX 查询执行并从服务器检索结果。当来自服务器的消息到达时,您的 ajaxIt 函数已经完成工作。

这里你应该使用的是延续传递风格。为 ajaxIt 提供延续:一个解释 ajaxIt 完成工作后应该执行的操作的函数。

function ajaxIt(data, continuation) {
  data.ajax = true;
  $.post("ajax;php", data, function(msg) {
    console.log("[AjaxIt]: returned "+msg);
    continuation(msg);
  }); 
}

ajaxIt({action:"logged-in"}, function(result) {
  if (result == "1") {
    console.log("Logged In");
    loggedIn=true;
    initiate2();
  }
});

$.ajax is asynchronous. This means that it will return right away, instead of waiting for the AJAX query to execute and retrieve a result from the server. By the time the message from the server arrives, your ajaxIt function has already finished working.

What you should use here is a continuation-passing style. Provide ajaxIt with a continuation: a function that explains what should be done once ajaxIt has finished working.

function ajaxIt(data, continuation) {
  data.ajax = true;
  $.post("ajax;php", data, function(msg) {
    console.log("[AjaxIt]: returned "+msg);
    continuation(msg);
  }); 
}

ajaxIt({action:"logged-in"}, function(result) {
  if (result == "1") {
    console.log("Logged In");
    loggedIn=true;
    initiate2();
  }
});
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文