我需要根据上一次调用的数据连续进行 6 次 ajax 调用.我将每个调用嵌套在上一个调用的成功中.我的问题是有什么好的方法来格式化代码,这样它就不会在我的编辑器中出现一百万行?
I have a requirement to make 6 ajax calls in succession based on data from the previous call. I am nesting each call in the success of the previous call. my question is what are some good ways to format the code so that it doesnt end up a million rows across my editor?
$.ajax({
type: "POST",
url: "someScript/someScript.php",
data: form + "&func=build",
success: function (result) {
if (result == "ok")
{
$.ajax({
type: "POST",
url: "someScript/someScript.php",
data: form + "&func=someOtherFunc",
success: function (result) {
if (result == "ok")
{
$.ajax({
type: "POST",
url: "someScript/someScript.php",
data: form + "&func=someOtherFunc",
success: function (result) {
if (result == "ok")
{
.....and so on
}
})
}
})
})
}
})
忽略括号,语法对于这个问题并不重要.
ignore brackets, syntax isnt important for this question.
你可以这样做
function ajaxCall1(){
$.ajax({
success: function(){
ajaxCall2();
}
});
}
function ajaxCall2(){
$.ajax({
success: function(){
ajaxCall3();
}
});
}
function ajaxCall3(){
$.ajax({
success: function(){
ajaxCall4();
}
});
}
这篇关于jquery嵌套ajax调用格式化的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持html5模板网!
为什么我的网站在 IE 中崩溃?Why does my website crash in IE?(为什么我的网站在 IE 中崩溃?)
从 contentEditable div 中删除格式Remove formatting from a contentEditable div(从 contentEditable div 中删除格式)
格式化此 JavaScript 行Formatting this JavaScript Line(格式化此 JavaScript 行)
JavaScript 格式:大括号必须与 if/function/etc 关键字在JavaScript formatting: must braces be on the same line as the if/function/etc keyword?(JavaScript 格式:大括号必须与 if/function/etc 关键字在同
<tag></tag> 有什么区别?和&aWhat#39;s the difference between lt;taggt;lt;/taggt; and lt;tag /gt; in HTML?(lt;taggt;lt;/taggt; 有什么区别?和lt;标签/gt;在 HTML 中?)
jquery按长度限制文本jquery limit text by length(jquery按长度限制文本)