weixin_33725272 2016-10-26 08:00 采纳率: 0%
浏览 5

$ http清空响应

I am working on an AJAX Angular application that displays paginated lists of photos. Most of it works well, but I am faced with this one problem which I had never met before: once loaded, some of the data from the server disappears in the process of the $http call. After analysis, I believe something went wrong in the response transformation (see below).

In this case, the controller calls for a JSON object containing a list of ids (sorted by date) and the total number of photos in base. This call is made every time the user changes page.

On the first time (i.e., on page load), it works fine, but it goes wrong once the page changes. It does load data from the server, and returns said object: this object does give me the total - but provides an empty array instead of the required list of ids.


// The first call returns a valid data object like this one (example for 10 ids per page):
// { "mostRecent" : [ 156, 161, 169, 178, 179, 181, 182, 183, 192, 203 ], "total" : 1653 }
// The second time returns a similar object but with an empty array of size 10, as follows:
// { "mostRecent" : [], "total" : 1653 }

This controller function makes a call to a Photo service that communicates with my server.


app.controller( 'PhotoManagerController', [ '$scope', '$q', 'Photo', function ( $scope,  $q, Photo ) {

  /**
   * Returns a serie of ids for the page.
   * @param params Parameters for the AJAX call (pagination, etc...)
   * @param init whether it is the first time the function is called or not.
   * @return a promise returning the ids.
   */
  var retrieveIds = function ( params, init ) {

    return Photo.getIds( params.pagination )
      .then( function ( response ) {

        // At this point, the data is valid the first time and incomplete the second time.

        if ( init ) { total = response.data.total;  }

        var loadedIds = response.data.mostRecent;

        return $q( function ( resolve ) { resolve( loadedIds ); } );
      } );
  };

} );

The Photo Service:


app.factory( 'Photo', [ '$http', '$q', 'SERVER_ROUTES', function ( $http, $q, SERVER_ROUTES ) {

  return {

    getIds : function ( pagination ) {
      var params = {};
      if ( pagination ) { 
        params.params = { 'c' : pagination.c, 'p' : pagination.p }; 
      }
      return $http.get( url + SERVER_ROUTES.PHOTO.GET_IDS, params );
    }

  };

} );

I have checked all sorts of things:

  • The server does return the data when Photo.getIds() is called.
  • I wondered if something was wrong with the order of my async calls, but I could not find anything of the sort after having inserted breakpoints to check the process.

After analysis, I would think the problem comes from the transformResponse object of the $http. I changed the query to use $http({}) with a transformResponse parameter, and saw in this function that the response to the second call DID contain, at first, the expected object with a full array of ids.

I do not know what the origin of the problem is, mostly considering that it worked well the first time. I guess it is caused by one of $http's default transformResponse function, but which one?

  • 写回答

0条回答 默认 最新

    报告相同问题?

    悬赏问题

    • ¥15 fluent的在模拟压强时使用希望得到一些建议
    • ¥15 STM32驱动继电器
    • ¥15 Windows server update services
    • ¥15 关于#c语言#的问题:我现在在做一个墨水屏设计,2.9英寸的小屏怎么换4.2英寸大屏
    • ¥15 模糊pid与pid仿真结果几乎一样
    • ¥15 java的GUI的运用
    • ¥15 Web.config连不上数据库
    • ¥15 我想付费需要AKM公司DSP开发资料及相关开发。
    • ¥15 怎么配置广告联盟瀑布流
    • ¥15 Rstudio 保存代码闪退