【问题标题】:How to correctly return my data from AJAX call (AngularJS)如何从 AJAX 调用(AngularJS)中正确返回我的数据
【发布时间】:2017-01-30 18:27:22
【问题描述】:

我有一个视图调用下面的这个函数,它对我们的 API 进行 AJAX 调用 - 由于某种原因,当我使用 Firefox DOM 检查工具在 AngularScope 中查看它时,它总是返回“未定义”。

如果我检查“网络”选项卡,我可以看到该 URL 已被调用并且可以看到我期望的 JSON,然后我想返回 data.words JSON 数据,但这总是返回未定义?如果我删除 AJAX 调用并在最后一次返回中留下“静态”和“单词”,则按预期工作,所以我很清楚 AJAX 成功调用中的返回似乎不正确...有什么想法吗??

// 在 AngularJS 服务文件中

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
}

【问题讨论】:

  • 尝试解析数据成功回调:data= JSON.parse(data)
  • 嗨 Nishanth,这个应该放在哪里 - 我应该删除成功块中的当前返回
  • hmm 在成功块中使用它仍然没有乐趣.. :(
  • 你的 ajax 没有返回任何东西..这只是一个回调......

标签: javascript angularjs json ajax angularjs-service


【解决方案1】:

那是因为你的 ajax 没有返回任何东西。如果你想将它分配给你应该做的范围:

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) {

    });

【讨论】:

    【解决方案2】:

    我认为你没有重新调整 $http 结果。你能试试下面的代码吗?

    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
    }
    

    【讨论】:

    • 感谢 Umakanta,虽然这给了我同样的问题,但很好
    • 似乎对我不起作用我正在做一些愚蠢的事情我敢肯定,我会继续这样做:)
    【解决方案3】:

    问题是当您发送 http 请求时,需要一些时间来处理数据并将数据发送回您的 javascript 代码。但由于 javascript 是异步的,它不会等到响应返回。因此,您可以像 Umakanta Behera 建议的那样返回整个 http 请求,也可以使用回调函数来等待响应返回。

    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) {
    
        });
    }
    

    像这样调用这个函数

    this.getAccSignoffWords(function(data){
         console.log(data) // your http response 
    })
    

    【讨论】:

    • 你能指出第二个“调用”函数与我的代码相关的位置吗(是这个控制器、服务、视图等)谢谢
    • 从你的控制器调用这个
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-04-11
    • 1970-01-01
    • 1970-01-01
    • 2011-08-15
    相关资源
    最近更新 更多