【问题标题】:jQuery Ajax Request inside Ajax RequestAjax 请求中的 jQuery Ajax 请求
【发布时间】:2012-04-22 18:49:48
【问题描述】:

是否可以在另一个 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!');
    }
});

【问题讨论】:

  • 是的,有可能
  • 你能发布你到目前为止的代码吗?
  • 我已经编辑了我的问题,代码在上面

标签: jquery ajax google-maps instagram


【解决方案1】:

这是一个例子:

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

【讨论】:

  • 您确定这是最佳做法吗?我的朋友认为我应该使用一个标志变量并在外面用一个 setInterval 函数检查它
  • 这也可能是一个解决方案,但这涉及的工作量更少,也更容易。
  • 我无法表达这个答案对我尝试在获取成功中发帖有多大帮助。
  • 我赞成这个,但不应该在 .done 或 .complete 函数中完成以确保完成第一个请求吗?
  • @Daniel success 函数只会在 ajax 获取 200 OK 时运行,这确保了第一个请求返回的东西(希望)可用,但使用 done 也是一个好方法它,没有嵌套。查看更多here
【解决方案2】:

从“完成”调用第二个 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*/
           },
       });
    }
});

【讨论】:

  • 非常感谢,你的遮阳篷救了我呵呵
【解决方案3】:

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

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

【讨论】:

    【解决方案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');
        }
    
    });
    

    【讨论】:

      猜你喜欢
      • 2012-05-19
      • 2013-01-09
      • 2015-12-15
      • 2013-12-24
      • 2014-04-28
      • 2015-11-11
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多