Ajax请求中的jQuery Ajax请求

是否可以在另一个ajax请求中发出一个ajax请求?因为我需要来自第一个ajax请求的一些数据才能发出下一个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!');
    }
});
Bias Tegaralaga asked 2020-01-23T23:00:55Z
4个解决方案
76 votes

这是一个例子:

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

                }
            });
        }
    });
Tarek answered 2020-01-23T23:01:15Z
14 votes

从'complete'调用第二个ajax

这是例子

   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*/
           },
       });
    }
});
Nishad Up answered 2020-01-23T23:01:39Z
1 votes

这只是一个例子。 您可能希望根据自己的要求对其进行自定义。

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

有关更多详细信息:请参阅此

Ved answered 2020-01-23T23:02:03Z
0 votes
$.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');
    }

});
Yusnur Hidayah answered 2020-01-23T23:02:19Z
translate from https://stackoverflow.com:/questions/10089447/jquery-ajax-request-inside-ajax-request