2012-04-10 196 views
37

是否有可能在另一個Ajax請求中發出ajax請求? ,因爲我需要第一個ajax請求中的一些數據來發出下一個ajax請求。Ajax請求中的jQuery Ajax請求

首先,我使用Google Maps API獲取LAT & LNG,之後我使用該LAT & LNG請求Instagram API(基於搜索的位置)。

再一次,這是可能的,如果是的話如何?

$('input#search').click(function(e){ 
    e.preventDefault(); 
    var source=$('select[name=state] option:selected').text()+' '+$('select[name=city] option:selected').text()+' '+$('select[name=area] option:selected').text(); 
    var source=source.replace(/ /g, '+'); 
    if(working==false){ 
     working=true; 
     $(this).replaceWith('<span id="big_loading"></span>'); 
     $.ajax({ 
      type:'POST', 
      url:'/killtime_local/ajax/location/maps.json', 
      dataType:'json', 
      cache: false, 
      data:'via=ajax&address='+source, 
      success:function(results){ 
       // this is where i get the latlng 
      } 
     }); 
    } else { 
     alert('please, be patient!'); 
    } 
}); 
+6

是其可能 – Ved 2012-04-10 13:12:06

+1

你可以發佈你到目前爲止的代碼。 – 2012-04-10 13:15:04

+0

我編輯了我的問題,代碼高於 – 2012-04-10 13:20:01

回答

57

下面是一個例子:

$.ajax({ 
     type: "post", 
     url: "ajax/example.php", 
     data: 'page=' + btn_page, 
     success: function (data) { 
      var a = data; // This line shows error. 
      $.ajax({ 
       type: "post", 
       url: "example.php", 
       data: 'page=' + a, 
       success: function (data) { 

       } 
      }); 
     } 
    }); 
+0

您確定這是最佳做法嗎?我的朋友認爲我應該使用一個標誌變量,並使用一個setInterval函數在 – whamsicore 2016-02-02 02:23:44

+1

之外檢查它,這可能也是一個解決方案,但這需要更少的努力和更容易。 – Tarek 2016-02-03 04:29:45

+3

我無法表達這個答案對我來說是多麼有幫助,試圖在獲得成功的過程中發表一篇文章。 – Bruce 2016-09-24 04:50:56

1

這僅僅是一個例子。您可能希望按照您的要求對其進行定製。

$.ajax({ 
     url: 'ajax/test1.html', 
     success: function(data1) { 
     alert('Request 1 was performed.'); 
     $.ajax({ 
     type: 'POST', 
     url: url, 
     data: data1, //pass data1 to second request 
     success: successHandler, // handler if second request succeeds 
     dataType: dataType 
    }); 
    } 
}); 

有關詳細信息:請參見this

7

試試這個

var dt=''; 
    $.ajax({ 
    type: "post", 
    url: "ajax/example.php", 
    data: 'page='+btn_page, 
    success: function(data){ 
       dt=data; 
       /*Do something*/ 
      }, 
    complete:function(){ 
      $.ajax({ 
      var a=dt; // This line shows error. 
      type: "post", 
      url: "example.php", 
      data: 'page='+a, 
      success: function(data){ 
       /*do some thing in second function*/} 
      }, 

    }); 
}); 
0
$.ajax({ 
    url: "<?php echo site_url('upToWeb/ajax_edit/')?>/" + id, 
    type: "GET", 
    dataType: "JSON", 
    success: function (data) { 
     if (data.web == 0) { 
      if (confirm('Data product upToWeb ?')) { 
       $.ajax({ 
        url: "<?php echo site_url('upToWeb/set_web/')?>/" + data.id_item, 
        type: "post", 
        dataType: "json", 
        data: {web: 1}, 
        success: function (respons) { 
         location.href = location.pathname; 
        }, 
        error: function (xhr, ajaxOptions, thrownError) { // Ketika terjadi error 
         alert(xhr.responseText); // munculkan alert 
        } 
       }); 
      } 
     } 
     else { 
      if (confirm('Data product DownFromWeb ?')) { 
       $.ajax({ 
        url: "<?php echo site_url('upToWeb/set_web/')?>/" + data.id_item, 
        type: "post", 
        dataType: "json", 
        data: {web: 0}, 
        success: function (respons) { 
         location.href = location.pathname; 
        }, 
        error: function (xhr, ajaxOptions, thrownError) { // Ketika terjadi error 
         alert(xhr.responseText); // munculkan alert 
        } 
       }); 
      } 
     } 
    }, 

    error: function (jqXHR, textStatus, errorThrown) { 
     alert('Error get data from ajax'); 
    } 

}); 
+0

只是一個例子,我用...並且工作... – 2017-07-30 02:59:56