javascript - AngularJS 进行 REST 调用并在浏览器中显示结果

标签 javascript angularjs rest

我正在尝试从 REST 服务获取数据并在浏览器中显示元素。 一段代码:

Server.prototype.typeNames = function() {
                var json = '{'
                        + '"query" : "MATCH n RETURN n.typeName LIMIT 10"'
                        + '}';
                $http.post("http://localhost:7474/db/data/cypher", json).then(function(response) {
                    var data = response.data;
                    console.log(data);
                    return data;
                });
            }

我确信 JSON 和 REST 调用是正确的,因为我可以在控制台中看到数据并且它是正确的,但我在页面上看不到它。 我的 js 文件中也有这个:

$scope.typeNames = Server.typeNames()

在 HTML 中:

{{typeNames}}

另外:我为 chrome 安装了 AngularJS 调试扩展,但它也是空的:

typeNames: null

在这里(http://pastebin.com/itHv97da)您可以找到更大的代码部分。这段代码不是我的,Server.prototype 的东西都在那里。我刚刚添加了 Server.prototype.typeNames,我正在尝试让它工作。

最佳答案

您的 typeNames 函数没有返回任何内容。

根据版本,您可以尝试返回整个 $http 部分,但更好的方法是返回一个 promise ,并在 .then 回调中解决该 promise 。

类似这样的:

var deferred = $q.defer();
$http.post("http://localhost:7474/db/data/cypher", json).then(function(response) {
    var data = response.data;
    console.log(data);
    deferred.resolve(data); // <--- this is the key
    return data;
});
return deferred.promise;

关于javascript - AngularJS 进行 REST 调用并在浏览器中显示结果,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24382909/

相关文章:

javascript - 如何在提供者的配置中处理 $rootScope

javascript - 使用 REST 服务和 Javascript 存储 token 的 Linkedin 登录

javascript - Node js中向rest服务发送https请求的步骤

java - JAX-RS 2 : using Guava ListenableFuture for async resources

javascript - JQuery 中的产品加载错误

javascript - 使用 lodash 检查对象中是否存在键

javascript - 允许用户单击 select2 并保留自由文本

javascript - 当 url 上已有 # anchor 时如何滚动到特定元素

javascript - 如何将ng-repeat中的数据与不同的索引绑定(bind)

php - Angular - PHP API 没有变量插入到数据库中