【发布时间】:2017-07-22 21:09:12
【问题描述】:
我是 AngularJS 的新手,在使用了几个 youtube 教程中显示的命令并阅读了文档之后,我似乎无法使用 $http.get() 请求在 API 上显示数据。
JavaScript 和 html 代码:
var exampleApp= angular.module('app', ['ionic']);
exampleApp.controller('exampleController', function($scope, $http){
$scope.getData=function(){
$http.get("https://p3ddibjuc6.execute-api.us-west-2.amazonaws.com/prod/entries")
.success(function(data){
$scope.Date= data.Date;
$scope.message= data.message;
})
.error(function(data){
alert("error");
})
};
} );
!DOCTYPE html>
<html ng-app="app">
<head>
<meta charset="utf-8">
<meta name="viewport" content="initial-scale=1, maximum-scale=1, user-scalable=no, width=device-width">
<link href="https://code.ionicframework.com/1.0.0/css/ionic.min.css" rel="stylesheet">
<script src="https://code.ionicframework.com/1.0.0/js/ionic.bundle.js"></script>
</head>
<body ng-app="app">
<ion-pane>
<ion-header-bar class="bar-stable">
<h1 class="title">Awesome App</h1>
</ion-header-bar>
<ion-content class="padding" ng-controller="exampleController">
<button class="button button-assertive" ng-click="getData()">click</button>
<br>
MESSAGE: {{message}}
<br>
Date: {{Date}}
</ion-content>
</ion-pane>
</body>
</html>
enter code here
【问题讨论】:
-
我无法从 API 获取数据以显示在 html 屏幕上
-
api 的响应。 (data) 有属性 Date?
-
@JesusCarrasco API (p3ddibjuc6.execute-api.us-west-2.amazonaws.com/prod/entries) 存储有关我要显示的“日期”和“消息”的数据。
-
呃,没有。看看它返回了什么。 message 是 Items 属性中的一个对象的属性,而不是顶级 json 对象的属性。
-
@KeshavSharma 你能发布 api 的答案吗,我的代理有问题。
标签: javascript angularjs ionic-framework angularjs-http