Reputation: 1355
In the promise then function when you receive the data object it is wrapped with another data object like
data = Object {data: Object, status: 200, config: Object, statusText: "OK"}
How to avoid this. you need to access your variables like data.data.myVar
var test123 = $scope.test();
test123.then(function(data){
console.log(data);
// why you need to access your data in "data.data.myValue"
},function(data){
});
$scope.test = function(){
var promise = $http(
{
method: 'GET',
dataType: "jsonp",
url: 'json/requestKey.json'
}
)
.success(function(data) {
return data;
})
.error(function(data){
//return data;
});
return promise;
};
Upvotes: 0
Views: 178
Reputation: 245
When you are returning, HttpResponseMessage, REST api response data in this format,
Object {data: Object, status: 200, config: Object, statusText: "OK"}
To parse in ajax success call back,
$http.get(urlBase + '/get/' + id).then(function (response) {
var yourData = response['data'];
var yourStatusCode = response['status'];
var yourStatusText = response['statusText'];
//assign data to any other $scope object
$scope.product = yourData;
}, function (error) {
console.log('error occured!!');
});
You will do fine now, you don't need to change the response from web api now.
Upvotes: 0
Reputation: 8394
Just return the data part from your "service". And you can ditch the redundant promise since $http
is already a promise.
$scope.test().then(function(data) {
console.log(data);
});
$scope.test = function() {
return $http('json/requestKey.json').then(function(response) {
return response.data;
});
};
Upvotes: 1
Reputation: 6002
Well this is be resolved two ways
success()
callback of your $http
, return data.data
instead of just return data
, so that in your then()
function you will get the internal object. Upvotes: 0