Py学习  »  Jquery

jquery同步调用

Programmer • 5 年前 • 1495 次点击  

为了呈现一个对话框,我有两个jquery ajax调用。一个用于加载按钮,另一个用于加载对话框主体。我首先调用加载按钮的函数(Asychronous Ajax调用)

 $.ajax({
    type: "POST",
    //async: false,
    url: action,
    dataType: "json",
    contentType: "application/json; charset=utf-8",
    success: function (result) {
        $('#dialogButtons').html(result);         
    },
    error: function (req, status, error) {
        alert(req + " " + error + " " + status);
    }

然后我调用另一个类似的Ajax调用以不对称方式加载对话框的主体。

按钮并不总是出现。所以我做了

$.ajaxSetup({ async: false });

$.ajax({
   asyc: false,
   url: action 
})

$.ajaxSetup({ async: true });

根据其他栈溢出专家。我对这种方法的看法不一。

请用标准方法帮助我实现这一点。

Python社区是高质量的Python/Django开发社区
本文地址:http://www.python88.com/topic/39060
 
1495 次点击  
文章 [ 1 ]  |  最新文章 5 年前
Barmar
Reply   •   1 楼
Barmar    6 年前

执行中的第二个Ajax调用 success 第一个函数。

$.ajax({
  type: "POST",
  url: action,
  dataType: "html",
  success: function(result) {
    $('#dialogButtons').html(result).hide(); // Will show it after 2nd AJAX call
    $.ajax({
      type: "POST",
      dataType: "html",
      url: otheraction,
      success: function(result) {
        if (result) {
          $("#dialog").html(result);
          $("#dialogButtons").show();
        }
      }
    });
  },
  error: function(req, status, error) {
    alert(req + " " + error + " " + status);
  }
});

你也不应该 contentType: "application/json" . 您没有发送任何邮件数据,并且 $.ajax 不发送JSON;如果您有POST数据,它将被URL编码。