php - 完成后在ajax之后获取变量

标签 php jquery ajax

 $('#submit_id').click(function(){
    function mark() {
        $.ajax({
          type: "POST",
          url: "request1.php",
          data: $('#form_id').serialize(),
          cache: false,
          success: function(data1){
          }
        });
    }
    function other() {
        $.ajax({
          type: "POST",
          url: "request2.php",
          data: $('#form_id').serialize(),
          cache: false,
          success: function(data2){
          }
        });
    }
    $.when(mark(), other()).done(function(data1, data2)
    {
        console.log(data1);
        $(".results1").html(data1);
        console.log(data2); // Result
        $(".results2").html(data2);

    });

});

我需要将返回的数据传递到一个变量中,例如:

console: undefined

最佳答案

你必须从你的函数中返回 promise 接口(interface):

function mark() {
        return $.ajax({
          type: "POST",
          url: "request1.php",
          data: $('#form_id').serialize(),
          cache: false
        });
    }

而且您不需要在此处指定成功回调(但您仍然可以)。

关于php - 完成后在ajax之后获取变量,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18075481/

相关文章:

php - 选择没有 JS 的提交按钮

php - 调用未定义的方法 Illuminate\Auth\GenericUser::fill()::when update

javascript - 如何从异步调用返回响应?

jquery - 使用 jquery ui 可排序插件获取项目的开始位置

php - $_POST 请求期间数据中断

javascript - 你能在 $.post() 中使用 $(this) 吗?

javascript - 在任何 ajax 回调中出现运行时错误后,ajaxStop 事件停止触发

php - 简化mysql过滤查询

php - 同一台服务器上的多个 gearman worker (PHP)

javascript - 在模态内显示图像,即使它是相同的图像