2012-04-10 32 views
37

È possibile effettuare una richiesta Ajax all'interno di un'altra richiesta Ajax? perché ho bisogno di alcuni dati dalla prima richiesta di ajax per effettuare la prossima richiesta Ajax.jQuery Ajax Richiesta interna Ajax

Prima Sto utilizzando Google Maps API per ottenere LAT & LNG, dopo che uso che LAT & LNG per richiedere Instagram API (ricerca basati sulla localizzazione).

Ancora una volta, è possibile, e se sì, come?

$('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

Sì, è possibile – Ved

+1

Potresti inserire il codice che avete finora. –

+0

Ho modificato la mia domanda, il codice è superiore a –

risposta

57

Ecco un esempio:

$.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

Sei sicuro che sia la migliore pratica? Il mio amico pensa che dovrei usare una variabile flag e controllarla con una funzione setInterval esterna a – whamsicore

+1

Anche questa potrebbe essere una soluzione, ma questo richiede meno sforzo e molto più facile. – Tarek

+3

Non riesco a esprimere quanto sia stata utile questa risposta nel tentativo di fare un post all'interno del successo di un get. – Bruce

1

Questo è solo un esempio. Potrebbe piacerti personalizzarlo secondo le tue esigenze.

$.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 
    }); 
    } 
}); 

Per maggiori dettagli: vedi this

7

provare questo

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

solo esempio, che ho usato ... e funziona ... –