【问题标题】:AJAX method Jquery can't return dataAJAX方法Jquery无法返回数据
【发布时间】:2016-01-03 23:45:13
【问题描述】:

我无法在 Jquery 中返回 ajax 请求的值。这是我的代码:

    function ajaxUniversal(datos, url) {
    $.ajax({
        url: url,
        data: {
            valores: datos
        },
        type: "POST",
        dataType: "html",
        success: function (data) {
            console.log("Datos recibidos: "+data)
            return data; //This does not returns the data
        },
        error: function (errorThrown) {
            return false;
        }
    });
}

如果我将 return 语句添加到最后:

function ajaxUniversal(datos, url) {
    $.ajax({
        url: url,
        data: {
            valores: datos
        },
        type: "POST",
        dataType: "html",
        success: function (data) {
            console.log("Datos recibidos: "+data)
            return data;
        },
        error: function (errorThrown) {
            return false;
        }
    });
    return data;//This is the statement but not works
}

我得到这个错误: 未捕获的 ReferenceError:未定义数据 如何返回数据?谢谢你。抱歉我的英语不好,但我会说西班牙语。

【问题讨论】:

  • 你可以尝试添加 async:false 选项

标签: javascript jquery ajax methods return


【解决方案1】:

Ajax 调用是异步的,因此您不能使用它们返回数据。如果要使用该数据,则需要使用回调函数。

function ajaxUniversal(datos, url, callback) {
    $.ajax({
        url: url,
        data: {
            valores: datos
        },
        type: "POST",
        dataType: "html",
        success: function (data) {
            console.log("Datos recibidos: "+data)
            callback(data);
        },
        error: function (errorThrown) {
            callback(errorThrown);
        }
    });
}

其他地方...

ajaxUniversal(someData, someUrl, function(data){
    // Do work with data here
    console.log(data);
});

【讨论】:

    【解决方案2】:

    您无法退回该商品,因为它不再存在。尝试先定义它,如下所示:

    function ajaxUniversal(datos, url) {
        var returlVal;
        $.ajax({
            url: url,
            async: false, 
            data: {valores: datos},
            type: "POST",
            dataType: "html",
            success: function (data) {
                console.log("Datos recibidos: "+data)
                returlVal = data;
            },
            error: function (errorThrown) {
                returlVal = false;
            }
        });
        return returlVal;
    }
    

    【讨论】:

    • 这行不通。 returlVal 将始终返回 undefined,因为 $.ajax 是异步的。在遇到return 语句时,它还没有获取到所需的数据。
    • 这种代码我已经写过几次了,它确实有效。如果你愿意,你可以试试。我也喜欢你的想法。可能会更好
    • @sg.cc 之所以有效,是因为 LiranBo 使其同步(参见 aysnc: false),但 imo sg.cc 的解决方案更好,因为 JS 的世界经常围绕回调的概念
    • 谢谢。我评论时async:false 不在。
    【解决方案3】:

    Ajax 调用是异步的,因此您不能立即从它们返回值。相反,他们返回一个返回值的承诺,所以你可以做的是:

    function ajaxUniversal(datos, url, callback) {
    return $.ajax({
        url: url,
        data: {
            valores: datos
        },
        type: "POST",
        dataType: "html"
    });
    }
    

    然后这样称呼它:

    ajaxUniversal( datos, url, callback ).then( function(data){
    
         //manipulate data here
    
    });
    

    【讨论】:

      【解决方案4】:

      正如其他人所说,由于请求是异步的,这将失败。您可以按照他们的建议修复您的代码,通过异步处理它,或者您可以使用async: false 将您的请求设置为同步。

      function ajaxUniversal(datos, url) {
          var data;
          $.ajax({
              url: url,
              async: false,  // <---- this will cause the function to wait for a response
              data: {
                  valores: datos
              },
              type: "POST",
              dataType: "html",
              success: function (data) {
                  console.log("Datos recibidos: "+data)
                  data = data;
              }
          });
          return data;
      }
      

      【讨论】:

        猜你喜欢
        • 2012-11-20
        • 1970-01-01
        • 2013-08-02
        • 1970-01-01
        • 1970-01-01
        • 2011-08-30
        • 2015-02-03
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多