AngularJS 1.2.0 $资源响应为空 [英] AngularJS 1.2.0 $resource response is empty

查看:99
本文介绍了AngularJS 1.2.0 $资源响应为空的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用AngularJS 1.2.0。
当我打电话与$种源web服务响应变量总是空的。 WebService的正确调用和web服务将结果发送到浏览器。的问题是,(回调(响应){})反应总是空的。

  angular.module(应用)。工厂('天气预报',['$资源',函数($资源){
返回$资源('http://open.mapquestapi.com/geocoding/v1/address',{键:getGeoKey()},{
    定位:{方法:GET,IsArray的:假}
});
}]);$ scope.getLocations =功能(LOCATIONNAME){
    返回geoCoding.locate({位置:LOCATIONNAME})$ promise.then(
      功能(响应){
        log.log('回应',响应);
        如果(应答放大器;&放大器; response.data&放大器;&放大器; response.data.results&放大器;&放大器; response.data.results [0]){          无功位置= [];          response.data.results [0] .locations.forEach(功能(位置){
            locations.push({名称:geoUtils.location2St​​ring(位置),位置:位置});
          });          返回地点;
        }其他{
          返回{};
        }      },功能(错误){
        log.error(位置查询错误:',错误);
      });
  };


解决方案

下面是使用1.2.0rc1几个不同的方法。

  VAR应用= angular.module('应用',['ngResource']);
app.factory('天气预报',['$资源',函数($资源){
  返回$资源('response.json',{键:123},{
    定位:{方法:GET,IsArray的:假}
  });
}]);app.controller('MainCtrl',函数($范围,地理编码){
  //直接将其绑定到异步结果
  $ scope.locations = geoCoding.locate({位置:位置});  //使用一个函数来触发请求
  $ scope.getLocations =功能(){
    $ scope.resp = geoCoding.locate({位置:位置});
  };  //使用函数使用的承诺触发请求
  $ scope.getLocationsAlt =功能(){
    geoCoding.locate({位置:位置})$ promise.then(功能(响应){
      $ scope.respAlt =响应;
    },angular.noop);
  };
});

基于注释更新

看起来你所面临的问题是,数据格式异常回来。这里是更新code会是什么样子( http://plnkr.co ?/编辑/ xzZHvm p = preVIEW ):

  VAR应用= angular.module('应用',['ngResource']);
app.factory('天气预报',['$资源,$ HTTP,$日志',函数($资源,$ HTTP,$日志){
  返回$资源('http://open.mapquestapi.com/geocoding/v1/address',{},{
    定位:{方法:GET,IsArray的:真实,transformResponse:$ http.defaults.transformResponse.concat(功能(数据,headersGetter){
      //可能会更好,如果你在这里研究的结果
      $ log.info(data.results [0]);
      返回data.results [0] .locations;
    })}
  });
}]);
app.controller('MainCtrl',函数($范围,$日志,地理编码){
  $ scope.locations = geoCoding.locate({位置:达拉斯,德克萨斯});
});

I'm using AngularJS 1.2.0. When I'm calling a webservice with $resouce the response variable is always empty. The webservice is called correctly and the webservice is sending the result to the browser. The problem is that (callback(response){}) response is always empty.

angular.module('app').factory('geoCoding',['$resource', function ($resource) {
return $resource('http://open.mapquestapi.com/geocoding/v1/address', {key: getGeoKey()}, {
    locate: {method: 'GET', isArray: false}
});
}]);

$scope.getLocations = function (locationName) {
    return geoCoding.locate({location: locationName}).$promise.then(
      function (response) {
        log.log('response ', response);
        if (response && response.data && response.data.results && response.data.results[0]) {

          var locations = [];

          response.data.results[0].locations.forEach(function (location) {
            locations.push({name: geoUtils.location2String(location), location: location});
          });

          return locations;
        } else {
          return {};
        }

      }, function (error) {
        log.error('Locations Query error: ', error);
      });
  };

解决方案

Here are a few different approaches using 1.2.0rc1.

var app = angular.module('app', ['ngResource']);
app.factory('geoCoding',['$resource', function ($resource) {
  return $resource('response.json', {key: 123}, {
    locate: {method: 'GET', isArray: false}
  });
}]);

app.controller('MainCtrl', function($scope,geoCoding){
  // bind it directly to the async result
  $scope.locations = geoCoding.locate({location: "a location"});

  // use a function to trigger the request
  $scope.getLocations = function () {
    $scope.resp = geoCoding.locate({location: "a location"});
  };

  // use a function to trigger the request using the promise
  $scope.getLocationsAlt = function() {
    geoCoding.locate({location: "a location"}).$promise.then(function(response){
      $scope.respAlt = response;
    }, angular.noop);
  };
});

Updated based on comment

It looks like the problem you are facing is that the data is coming back in an unexpected format. Here is what the updated code would look like (http://plnkr.co/edit/xzZHvm?p=preview):

var app = angular.module('app', ['ngResource']);
app.factory('geoCoding',['$resource', '$http', '$log', function ($resource, $http, $log) {
  return $resource('http://open.mapquestapi.com/geocoding/v1/address', {}, {
    locate: {method: 'GET', isArray: true, transformResponse: $http.defaults.transformResponse.concat(function(data, headersGetter) {
      // probably be better if you examined the results in here
      $log.info(data.results[0]);
      return data.results[0].locations;
    })}
  });
}]);
app.controller('MainCtrl', function($scope, $log, geoCoding){
  $scope.locations = geoCoding.locate({location: "Dallas, TX"});
});

这篇关于AngularJS 1.2.0 $资源响应为空的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆