使用$ .when组合AJAX请求并将数据发送到函数

时间:2015-02-24 17:06:45

标签: javascript jquery ajax json multiple-instances

我有功能和AJAX调用更新页面中需要分开的内容,但是在页面加载时他们需要全部加载,因此我使用$ .when - 但我理解在函数之间发送数据和使用$当糟糕的时候 - 我已经看了几个结合AJAX调用的解决方案,但似乎都失败了。这是我目前的解决方案:

  1. 我有2个运行单独AJAX请求的函数。
  2. 然后我从$ .when
  3. 打电话给我们
  4. 返回数据并采用正确的JSON格式(在JSONlint上验证) 然而,当通过$。传回时,数据以某种方式未设置/丢失,此时我不明白发生了什么。 我可以在AJAX函数中输出数据,但如果我尝试从$ .when函数或onload_sendData函数中输出,数据总是变为" undefined"。
  5. 我在传递给函数之前尝试过JSON.stringify和JSON.parse,然后 - 没有区别,当我尝试以这种方式传递给下一个函数后尝试读取数据时,数据总是变得不确定。也许我错过了Javascript如何传递数据的基本信息,希望有人可以提供帮助。

    function onload_Categories() {
        $.ajax({
                  type: "GET",
                  url: '/index/categories',
                  ContentType : 'application/json',
                  success: function (data1) {
                    return data1;
                  },
                  error: function (jqXHR, textStatus, errorThrown) {
                    return errortThrown;
                  }
              })
    }
    
    function onload_Subscribed() {
        return $.ajax({
                  type: "GET",
                  url: '/index/getsubscribed',
                  ContentType : 'application/json',
                  success: function (data2) {
                    return data2;
                  },
                  error: function (jqXHR, textStatus, errorThrown) {
                    return errortThrown;
                  }
              })
    }
    
    function onload_sendData(data1) {
      alert(data1); // "UNDEFINED"
      //processCategories(data1); // send data to populate the page categories
      //processSubscribed(data2); // send data to populate the subscription data
    }
    
      (function($){
          $(window).load(function(){
    

    v1

    $.when(onload_Categories(),onload_Subscribed()).then(onload_sendData);
    

    V2

    $.when(onload_Categories(),onload_Subscribed()).then(function(data1,data2){
    
    
        alert(data1); // "UNDEFINED"
        alert(JSON.parse(data1); // "UNDEFINED"
    
        })
    
        })
     })
    

    这里是从类别ajax函数中检索到的json数据的片段:

    > [{"categorytitle":"accessories","category_id":"56","parent":"7","count":0,"p_count":0,"ad_ids":0,"rem_ads_age":0,"rem_ads_loc":0,"hassub":0},{"categorytitle":"accessories","category_id":"64","parent":"9","count":0,"p_count":0,"ad_ids":0,"rem_ads_age":0,"rem_ads_loc":0,"hassub":0},{"categorytitle":"accessories","category_id":"72","parent":"11","count":0,"p_count":0,"ad_ids":0,"rem_ads_age":0,"rem_ads_loc":0,"hassub":0},{"categorytitle":"activities","category_id":"57","parent":"7","count":0,"p_count":0,"ad_ids":0,"rem_ads_age":0,"rem_ads_loc":0,"hassub":0},{"categorytitle":"activities","category_id":"81","parent":"15","count":0,"p_count":0,"ad_ids":0,"rem_ads_age":0,"rem_ads_loc":0,"hassub":0},{"categorytitle":"appliances","category_id":"83","parent":"16","count":0,"p_count":0,"ad_ids":0,"rem_ads_age":0,"rem_ads_loc":0,"hassub":0},{"categorytitle":"apps","category_id":"4","parent":"0","count":2,"p_count":2,"ad_ids":"25,27,","rem_ads_age":0,"rem_ads_loc":0,"hassub":0},{"categorytitle":"auto","category_id":"18","parent":"0","count":1,"p_count":3,"ad_ids":"27,","rem_ads_age":0,"rem_ads_loc":0,"hassub":1},{"categorytitle":"bags","category_id":"32","parent":"1","count":0,"p_count":0,"ad_ids":0,"rem_ads_age":0,"rem_ads_loc":0,"hassub":0},{"categorytitle":"belts","category_id":"25","parent":"1","count":0,"p_count":0,"ad_ids":0,"rem_ads_age":0,"rem_ads_loc":0,"hassub":0},{"categorytitle":"body
    > care","category_id":"3","parent":"0","count":0,"p_count":1,"ad_ids":0,"rem_ads_age":0,"rem_ads_loc":0,"hassub":1},{"categorytitle":"body
    > work and
    > \n\nrepair","category_id":"101","parent":"18","count":0,"p_count":0,"ad_ids":0,"rem_ads_age":0,"rem_ads_loc":0,"hassub":0},{"categorytitle":"books","category_id":"39","parent":"2","count":0,"p_count":0,"ad_ids":0,"rem_ads_age":0,"rem_ads_loc":0,"hassub":0},{"categorytitle":"business","category_id":"19","parent":"0","count":0,"p_count":0,"ad_ids":0,"rem_ads_age":0,"rem_ads_loc":0,"hassub":0},{"categorytitle":"Business
    > insurance","category_id":"110","parent":"105","count":0,"p_count":0,"ad_ids":0,"rem_ads_age":0,"rem_ads_loc":0,"hassub":0},{"categorytitle":"cafes","category_id":"58","parent":"8","count":0,"p_count":0,"ad_ids":0,"rem_ads_age":0,"rem_ads_loc":0,"hassub":0},{"categorytitle":"cameras","category_id":"88","parent":"16","count":0,"p_count":0,"ad_ids":0,"rem_ads_age":0,"rem_ads_loc":0,"hassub":0},{"categorytitle":"car
    > hire","category_id":"99","parent":"18","count":1,"p_count":1,"ad_ids":"20,","rem_ads_age":0,"rem_ads_loc":0,"hassub":0},{"categorytitle":"cars","category_id":"98","parent":"18","count":1,"p_count":1,"ad_ids":"20,","rem_ads_age":0,"rem_ads_loc":0,"hassub":0},{"categorytitle":"computers","category_id":"86","parent":"16","count":0,"p_count":0,"ad_ids":0,"rem_ads_age":0,"rem_ads_loc":0,"hassub":0},{"categorytitle":"computers
    > and
    > tablets","category_id":"90","parent":"16","count":0,"p_count":0,"ad_ids":0,"rem_ads_age":0,"rem_ads_loc":0,"hassub":0},{"categorytitle":"consoles
    > and
    > \n\ngames","category_id":"89","parent":"16","count":0,"p_count":0,"ad_ids":0,"rem_ads_age":0,"rem_ads_loc":0,"hassub":0},{"categorytitle":"cruises","category_id":"63","parent":"9","count":0,"p_count":0,"ad_ids":0,"rem_ads_age":0,"rem_ads_loc":0,"hassub":0},{"categorytitle":"cufflinks","category_id":"27","parent":"1","count":0,"p_count":0,"ad_ids":0,"rem_ads_age":0,"rem_ads_loc":0,"hassub":0}]
    

    只是为了彻底,并且为了您的理解 - 下面的IS工作在函数之间传递数据:

    function getCategories($c) {
    
      $.when(
    
          $.get('/index/categories')
    
      ).then(function(categories) {
    
        processCategories(categories, $c);
    
      });
    
    }
    
    function processCategories(categories, $c) {
    
    var jsonData = JSON.parse(categories); // data OK
    
    etc...
    

1 个答案:

答案 0 :(得分:0)

对于任何可能都很好奇的人来说,AJAX需要特别回归:

return $.ajax({
              type: "GET",
              url: '/index/getsubscribed',
              ContentType : 'application/json',
})

并使用此数据,您需要引用第一个索引,因为AJAX响应是一个数组。

        $.when(
          onload_Categories(),
          onload_Subscribed()
          ).then(function(data1,data2) {
            var categories = data1[0]; 

// 0 - is your data, 1 - AJAX fail/success response, 2 - other ajax response object data.
              processCategories(categories);
            var subscribed = data2[0];
              processSubscribed(subscribed);

          })

可能是基本的东西,但是我失去了几个小时的生命,因为我对此很新,希望它对某人有所帮助。

更新:

我的服务器在单个$ .when语句中不会接受2 GETS - 第2个总会收到500个内部服务器错误 - 我看到没有身体堆叠了$ .when语句,但是这个似乎它是唯一的解决方案,对我来说效果很好......

$.when(
          onload_Categories()
          ).then(function(data) {
            var categories = data;
            alert(categories);
              processCategories(categories);
              $.when(
                onload_Subscribed()
                ).then(function(data) {
                  var subscribed = data;
                  alert(subscribed);
                    processSubscribed(subscribed);
                })
          })