我的http服务(UserService)中有以下代码:
module1.factory('UserService', ['$http', '$q', function($http, $q) {
return {
getModelValueTableValues1: function(CarLine, GeographyType, GeographyName, Duration, ModelId, ScenarioId, TimeKey) {
return $http.get('http://localhost:8080/scenarioplan/models/data?ScenarioId=5&CarLine=Nissan%20-%20Rogue&TimeKey=Jul-16&Duration=1&Geographytype=National&GeographyName=National&ModelId=1001')
.then(
function successCallback(response) {
return response.data;
},
function errorCallback(errResponse) {
console.error('Error while fetching Model Parameter Table');
return $q.reject(errResponse);
}
);
},
我的控制器中有以下代码:
$scope.getModelValueTableValues = function(CarLine, Geography, Duration, ModelId, ScenarioId) {
UserService.getModelValueTableValues1(CarLine, Geography.split('-')[0], Geography.split('-')[1], Duration, ModelId, ScenarioId, '16-Jul')
.then(
function(d) {
console.log("the data object from promise is", d);
console.log("the data object from promise is", d.length);
console.log('The Promise has been successfully resolved!!!!');
},
function(errResponse) {
console.error('The Promise was not successfull');
}
);
};
但是,在获得解析后,promise不会返回数据数组,而是提供一个对象数组,如下图所示,在我的调试窗口中(F12)。
来自Web服务呼叫的响应似乎是有效的数据,如下面的F12控制台输出所示:
有人可以帮我解决这个问题吗? 我一直试图解决这个问题,但没有任何成功!
答案 0 :(得分:1)
$ http实现承诺。你的功能
return $http.get('http://localhost:8080/scenarioplan/models/data?ScenarioId=5&CarLine=Nissan%20-%20Rogue&TimeKey=Jul-16&Duration=1&Geographytype=National&GeographyName=National&ModelId=1001');
或者如果你想在函数内部使用过程数据。
return {
getModelValueTableValues1:function(CarLine,GeographyType,GeographyName,Duration,ModelId, ScenarioId,TimeKey){
var deffered = $q.defer();
$http.get('http://localhost:8080/scenarioplan/models/data?ScenarioId=5&CarLine=Nissan%20-%20Rogue&TimeKey=Jul-16&Duration=1&Geographytype=National&GeographyName=National&ModelId=1001')
.then(
function successCallback(response){
deffered.resolve(response);
},
function errorCallback(errResponse){
console.error('Error while fetching Model Parameter Table');
deffered(errResponse);
}
);
return deffered.promise;
},
答案 1 :(得分:0)
以下是我为解决问题而采取的完整步骤。
根据@vanmohit的建议: 我在我的appfactory服务中使用了他的代码:
return {
getModelValueTableValues1: function(CarLine,GeographyType,GeographyName,Duration,ModelId, ScenarioId,TimeKey){
var deffered = $q.defer();
$http.get('my url here')
.then(
function successCallback(response){
deffered.resolve(response);
},
function errorCallback(errResponse){
console.error('Error while fetching Model Parameter Table');
deffered(errResponse);
}
);
return deffered.promise;
},
在我的控制器代码中,我使用了这个函数:
function(CarLine, Geography,Duration,ModelId, ScenarioId ){
UserService.getModelValueTableValues1(CarLine,Geography.split('-')[0],Geography.split('-')[1],Duration,ModelId,ScenarioId,'16-Jul')
.then(
function(d) {
console.log("the data object from promise is",JSON.parse(angular.toJson(d.data))[1].paramName);
//console.log("the data object from promise is",d.length);
//JSON.parse(angular.toJson(resp))
$scope.firstrow = JSON.parse(angular.toJson(d.data));
console.log('The Promise has been successfully resolved!!!!');
},
function(errResponse){
console.error('The Promise was not resolved');
}
);
};
现在我能够通过控制器内的JSON数据进行解析! 非常感谢你的帮助!