2017-05-29 56 views
0

我发送ajax调用并从第一个ajax获得我需要的答案,然后我想将我的结果传递给我的嵌套ajax,我的var(result)为null嵌套的ajax/settimeout的乐趣,我可以通过它吗?我错过了什么吗?如何将变量从ajax传递给嵌套的ajax

$.ajax({ 
      url: '@Url.Action("getCustomerGuidId", "Document")', 
      type: 'POST', 
      cache: false, 
      data: { "classNum": currentclassNum}, 
      contentType:'json' , 
      dataType:'text', 
      success: function (result) { 
       alert(result);**-> is fine - not null**. 
// a or result is null when I hit the getCurrentDoc- function althought I get the data I need from getCustomerGuidId function 
       var a = result;-> tried to pass it to a new var..IDK.. I 
            thought it will help... it didn't. 
       setTimeout(function() { 
        $.ajax({ 
         type: "GET", 
         url: '@Url.Action("getCurrentDoc", "Document")', 
         contentType:'text', 
         data: a,-> here it's null 
         success: function (data) { 
         } 
        });             
       }, 2000);         
      }, 
      error: function (result) { 
       alert("fail " + result); 
      } 
     }); 
+0

在的setTimeout ,函数上下文得到了改变,这就是为什么a在那里是空的。 –

回答

-1

在嵌套的AJAX您发送的PARAM名称,而不是作为一个参数值的方式。 所以,你可以试试下面的(变化param到您的服务器预计实际PARAM名):

$.ajax({ 
     url: '@Url.Action("getCustomerGuidId", "Document")', 
     type: 'POST', 
     cache: false, 
     data: { "classNum": currentclassNum}, 
     dataType:'text', 
     success: function (result) { 
      setTimeout(function() { 
       $.ajax({ 
        type: "GET", 
        url: '@Url.Action("getCurrentDoc", "Document")', 
        data: {param: result}, 
        success: function (data) { 
        } 
       });             
      }, 2000);         
     }, 
     error: function (result) { 
      alert("fail " + result); 
     } 
    }); 
+0

仍然无效...: - / – Damkulul

0

你可以尝试这样的事情将有助于值传递给嵌套的AJAX调用

function test(){ 
var myText = 'Hello all !!'; 

$.get({ 
    //used the jsonplaceholder url for testing 
    'url':'https://jsonplaceholder.typicode.com/posts/1', 
    'method':'GET', 
    success: function (data) { 
     //updating value of myText 
     myText = 'welcome'; 
     $.post({ 
      'url':'https://jsonplaceholder.typicode.com/posts', 
      'method':'POST', 
      //data.title is the return value from get request to the post request 
      'data':{'title':data.title}, 
      'success':function (data) { 
       alert(data.title +'\n' + myText);//your code here ... 

      } 
     }); 
    } 
}); 

}