jQuery Ajax Request inside Ajax Request

泪湿孤枕 提交于 2019-11-26 09:21:10

问题


Is it possible to make an ajax request inside another ajax request? because I need some data from first ajax request to make the next ajax request.

First I\'m using Google Maps API to get LAT & LNG, after that I use that LAT & LNG to request Instagram API (search based location).

Once again, is this possible, and if so how?

$(\'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!\');
    }
});

回答1:


Here is an example:

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

                }
            });
        }
    });



回答2:


Call second ajax from 'complete'

Here is the example

   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*/
           },
       });
    }
});



回答3:


This is just an example. You may like to customize it as per your requirement.

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

For more details : see this




回答4:


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

});


来源:https://stackoverflow.com/questions/10089447/jquery-ajax-request-inside-ajax-request

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!