0

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;
};
1
  • It allows you to also access data.status or data.statusText apparently… What's wrong with that? Commented Aug 6, 2015 at 6:56

3 Answers 3

1

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;
  });
};
Sign up to request clarification or add additional context in comments.

Comments

0

Well this is be resolved two ways

  1. Try returning the resulting object instead of wrapping it around another object and then returning that to the client on the server side.
  2. In the 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.

5 Comments

You cannot return from a .success callback.
@Bergi: Isn't it same as jQuery.ajax ?
No, $http (angular) is different from $.ajax (jQuery). And even in jQuery you cannot return from success callbacks, you have to use then (or pipe in old versions).
The succes()callback should not be used anymore as it is deprecated since Angular 1.4.
@cverb:yeah i realised that
0

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.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.