0

I have a view that calls this function below which makes an AJAX call to our API - this for some reason always returns 'undefined' when I view this in the AngularScope using the Firefox DOM inspection tool.

If I check the Network tab I can see this URL has been called and can see the JSON which I expect, I then want to return the data.words JSON data but this is always returning undefined? If I remove the AJAX call and just leave in the last return with the 'static' and 'words' in this works as expected, so it is clear to me something about the returning in the AJAX success call doesn't seem to be right... any ideas??

// within a AngularJS service file

this.getAccSignoffWords = function() {
    var url = ApiService.getDomain() + 'account/signoff';
    $http({
        method : 'GET',
        url : url
    }).success(function(data) {
        if (data.success) {
            return data.words; // I want to return this JSON to the scope
        }
    }).error(function(data) {
        throw "Failed to get sign-off words";
    })['finally'](function(data) {

    });

    // return [ 'static', 'words' ]; // this line is commented out and only used for testing and is an example of the type of data expected from data.words
}
4
  • Try parsing the data in success call back: data= JSON.parse(data) Commented Sep 22, 2016 at 9:07
  • Hi Nishanth, where should this be placed - shall I remove the current return within the success block Commented Sep 22, 2016 at 9:09
  • hmm still no joy using that in the success block.. :( Commented Sep 22, 2016 at 9:13
  • your ajax doesn't return anything..that's just a call back... Commented Sep 22, 2016 at 9:16

3 Answers 3

1

thing is when you are sending the http request, it take some time to process and send data back to your javascript code. but since the javascript is asynchronous, it doesn't wait until response return. so either you can return the whole http request like Umakanta Behera suggested or you can use a callback function to wait unit the response come back.

this.getAccSignoffWords = function(callback) {
    var url = ApiService.getDomain() + 'account/signoff';
    $http({
        method : 'GET',
        url : url
    }).success(function(data) {
        if (data.success) {
            callback() data.words
        }
    }).error(function(data) {
        throw "Failed to get sign-off words";
    })['finally'](function(data) {

    });
}

call this function like this

this.getAccSignoffWords(function(data){
     console.log(data) // your http response 
})
Sign up to request clarification or add additional context in comments.

2 Comments

Can you point out where the second 'calling' function goes in relation to my code (is this controller, service, view etc..) thanks
Call this from ur controller
1

that's because your ajax doesn't return anything.. if you want to assign it to scope you should be doing:

var self = this;
var url = ApiService.getDomain() + 'account/signoff';
    $http({
        method : 'GET',
        url : url
    }).success(function(data) {
        if (data.success) {
            self.getAccSignoffWords = data.words; // I want to return this JSON to the scope
        }
    }).error(function(data) {
        throw "Failed to get sign-off words";
    })['finally'](function(data) {

    });

Comments

0

I think you are not retuning $http result.Could u please give a try to below code.

this.getAccSignoffWords = function() {
    var url = ApiService.getDomain() + 'account/signoff';
    return $http({
        method : 'GET',
        url : url
    }).success(function(data) {
        if (data.success) {
            return data.words; // I want to return this JSON to the scope
        }
    }).error(function(data) {
        throw "Failed to get sign-off words";
    })['finally'](function(data) {

    });

    // return [ 'static', 'words' ]; // this line is commented out and only used for testing and is an example of the type of data expected from data.words
}

2 Comments

thanks Umakanta although that gives me the same issue, good shot at it though
doesn't appear to work for me i'm doing something silly i am sure, i will keep on it :)

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.