如何在第一个AJAX的成功方法中制作第二个AJAX请求?

时间:2014-05-23 02:34:06

标签: jquery ajax asp.net-mvc

假设我有这个ajax电话。我正在请求包含2个字符串的json对象。如果第一个不为空,我将显示一个确认弹出窗口以警告用户,并决定是否继续。

如果用户决定请求该文件,则第二个字符串包含该文件的路径

那么如何在第一个AJAX的成功方法中发出第二个AJAX请求呢?

function reqQuote(_url)
{
   $.ajax({
      url: _url,
      success: function(data){
          if(data.itemsNotSupportedWarning != null){

         var r = Confirm('The template doesn\'t support the following languages: ' 
                         + data.itemsNotSupportedWarning + 
                         + ' Would you like to continue?');
            if(r == true)
            {
               //request the template file ...
            }
          }
          else{
             //there is no warning, i.e. just go ahead and request the template file.
         }
      }
  });
}

感谢您的帮助。

2 个答案:

答案 0 :(得分:2)

只需将另一个$.ajax(...)置于if(r == true) { ... }内,并为其定义另一个成功回调函数。

答案 1 :(得分:1)

在ajax调用的成功方法中调用一些其他javascript方法,你可以像普通的ajax一样调用ajax。见下文。

    function reqQuote(_url)
    {
       $.ajax({
          url: _url,
          success: function(data){
                        testMethod();
      }
  });
}

function testMethod(){
    //Ajax Code Here
    }