为了渲染对话框,我有两个jQuery ajax调用。一个加载按钮,另一个加载对话框的主体。我首先调用加载按钮的函数(异步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 });
根据其他堆栈溢出专家。我对这种方法意见不一。
请帮助我实现这一目标的标准方法。
答案 0 :(得分:0)
在第一个success
函数中进行第二个AJAX调用。
$.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;如果您有发布数据,则将使用URL编码。