我正在学校里做这个项目。他们要求我使用$ http.post输入2个值的Day和Customer并在表中显示json数据。您能帮我这个忙吗,我已经研究了如何使用$ http.post并修复代码,但无法正常工作。请告诉我我误解了什么,或者还有其他方法可以完成此操作。非常感谢。
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title> $http.post </title>
<script src="//code.angularjs.org/snapshot/angular.min.js"></script>
<script
src="http://ajax.googleapis.com/ajax/libs/angularjs/1.4.8/angular.min.js">
</script>
<style>
table,
th,
td {
border: 1px solid grey;
border-collapse: collapse;
padding: 5px;
}
table tr:nth-child(odd) {
background-color: #f5f5f5;
}
table tr:nth-child(even) {
background-color: #ffffff;
}
</style>
</head>
<body>
<div ng-app="postapp" ng-controller="postservice">
<div>
Day : <input ng-model="day"><br /><br />
Customer : <input ng-model="customer"><br /><br />
<input type="button" value="Send" ng-click="postdata(day, customer)">
<br /><br />
</div>
<table>
<tr>
<th>ID</th>
<th>Status</th>
<th>Environment</th>
<th>Host</th>
<th>IP</th>
<th>Description</th>
<th>Time</th>
</tr>
<tr ng-repeat="x in names">
<td>{{ x.ID}}</td>
<td>{{ x.Status}}</td>
<td>{{ x.Environment}}</td>
<td>{{ x.Host}}</td>
<td>{{ x.IP}}</td>
<td>{{ x.Description}}</td>
<td>{{ x.Time}}</td>
</tr>
</table>
</div>
<script>
var app = angular.module('postapp', []);
app.controller('postservice', function postservice($scope, $http) {
$scope.day = null;
$scope.customer = null;
$scope.postdata = function(day, customer) {
var data = {
day: day,
customer: customer
};
$http.post("https://b0gtju7vp5.execute-api.us-east-
1.amazonaws.com/staging", JSON.stringify(data))
.success(function(response) {
$scope.names = response;
});
};
});
</script>
</body>
这里是我的工作方式,希望这可以使它变得更加清晰。View pic
答案 0 :(得分:0)
对其进行了爵士化处理,并为您添加了一个加载微调器。
此外,由于请求是跨源的,即向与它所运行的网站不同的URL发出请求,您将需要在禁用CORS的情况下运行Chrome。 Chrome默认会阻止这些请求,您必须将其切换以进行开发。
在Windows的运行框中粘贴以下内容:
chrome.exe --user-data-dir="C://Chrome-dev-session" --disable-web-security
如果您使用的是Mac google,则“如何在禁用CORS的情况下运行Chrome”。
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title> $http.post </title>
<script src="//code.angularjs.org/snapshot/angular.min.js"></script>
<script src="http://ajax.googleapis.com/ajax/libs/angularjs/1.4.8/angular.min.js">
</script>
<style>
table,
th,
td {
border: 1px solid grey;
border-collapse: collapse;
padding: 5px;
}
table tr:nth-child(odd) {
background-color: #f5f5f5;
}
table tr:nth-child(even) {
background-color: #ffffff;
}
</style>
</head>
<body>
<div ng-app="postapp" ng-controller="postservice">
<!-- Loading spinner that will be shown when requst is in progres -->
<div ng-if="isLoading">
loading data...
</div>
<!-- Hide the page content when loading data -->
<div ng-if="!isLoading">
<div>
Day :
<input ng-model="day">
<br />
<br /> Customer :
<input ng-model="customer">
<br />
<br />
<input type="button" value="Send" ng-click="getNames(day, customer)">
<br />
<br />
</div>
<table>
<tr>
<th>ID</th>
<th>Status</th>
<th>Environment</th>
<th>Host</th>
<th>IP</th>
<th>Description</th>
<th>Time</th>
</tr>
<tr ng-repeat="name in names">
<td>{{ name.ID}}</td>
<td>{{ name.Status}}</td>
<td>{{ name.Environment}}</td>
<td>{{ name.Host}}</td>
<td>{{ name.IP}}</td>
<td>{{ name.Description}}</td>
<td>{{ name.Time}}</td>
</tr>
</table>
</div>
<pre>{{names | json}}</pre>
</div>
<script>
var app = angular.module('postapp', []);
app.controller('postservice', function postservice($scope, $http) {
$scope.day = null;
$scope.customer = null;
$scope.names = [];
$scope.isLoading = false;
$scope.getNames = function (day, customer) {
$scope.isLoading = true;
var data = {
day: day,
customer: customer
};
var url = "https://b0gtju7vp5.execute-api.us-east-1.amazonaws.com/staging";
$http.post(url, data)
.then(
function (response) {
$scope.names = response;
$scope.isLoading = false;
},
function (error) {
alert('Failed to post data');
$scope.isLoading = false;
});
};
});
</script>
</body>
</html>