【问题标题】:Make a ajax POST call again after the data in success is updated成功更新数据后再次进行ajax POST调用
【发布时间】:2017-10-28 18:16:22
【问题描述】:

我将根据发布的数据在此处获取对象。用户在数据中选择不同的过滤器。当用户选择不同的过滤器时,我想更新 postThis 对象并使用更新的对象再次调用 Ajax。

var postThis = {
  "name": ''
}
$.ajax({
  method:post,
  url:someurl,
  data: postThis
})
.success(function(data) {
  // name has to be updated with value which I get after user chooses it

}) 

【问题讨论】:

    标签: javascript jquery ajax object


    【解决方案1】:

    如果您只是想在第一个成功的情况下发送另一个 AJAX 请求,并带有第一个请求的结果,那么只需像您已经拥有的那样发出另一个 $.ajax 请求,并将接收到的数据传递给它:

    $.ajax({
      method: post,
      url: someurl,
      data: postThis
    })
    .success(function(data) {
      yourSecondAjaxCall(data);
    })
    
    function yourSecondAjaxCall(dataFromFirstAjax) {
      $.ajax({
        method: post,
        url: someurl,
        data: dataFromFirstAjax
      })
      .success(function(data) {
         // do whatever
      })
    }
    

    【讨论】:

      【解决方案2】:

      最简单的方法就是嵌套 ajax 调用

      $.ajax({
        method:post,
        url:someurl,
        data: postThis
      })
      .success(function(data) {
        // name has to be updated with value which I get after user chooses it
            $.ajax({
            method:post,
            url:someurl,
            data: UPDATED_DATA
          })
          .success(function(data) {
          [...]
          }) 
      }) 
      

      【讨论】:

        猜你喜欢
        • 2012-02-06
        • 2015-09-25
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2016-02-28
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多