html - 带有 POST 的 REST 调用不显示响应消息
问题描述
我正在尝试使用 AngularJS 进行 POST 调用,并通过获取响应消息、响应状态等来查看是否一切正常。
我想要的是看看我的 POST 是否正常并且服务调用没有问题。
编辑:语法错误已更正
angular.module('myApp', [])
.controller('myCtrl', function ($scope, $http) {
$scope.hello = {id: "1", name: "ERA"};
$scope.newId= "";
$scope.newName = "";
$scope.sendPost = function() {
var data = $.param({
json: JSON.stringify({
id: $scope.newId,
name: $scope.newName
})
});
$http.post("http://localhost:8080/academi-co/resources/tags/languages/", data).success(function(data, status) {
$scope.hello = data;
if (response.data)
$scope.msg = "Post Data Submitted Successfully!";
}, function (response) {
$scope.msg = "Service not Exists";
$scope.statusval = response.status;
$scope.statustext = response.statusText;
$scope.headers = response.headers();
})
}
})
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.2.23/angular.min.js"></script>
<body ng-app='myApp'>
<div ng-controller="myCtrl">
<form ng-submit="sendPost()">
<input ng-model="newId"/>
<input ng-model="newName"/>
<button type="submit">Send</button>
</form>
<p>{{hello.id}}</p>
<p>{{hello.name}}</p>
<p>Output Message : {{msg}}</p>
<p>StatusCode: {{statusval}}</p>
<p>Status: {{statustext}}</p>
<p>Response Headers: {{headers}}</p>
</div>
</body>
解决方案
我将代码更改为一些代码,也许试试这个。
angular.module('myApp', [])
.controller('myCtrl', function ($scope, $http) {
$scope.hello = {id: "1", name: "ERA"};
$scope.newId= "";
$scope.newName = "";
$scope.sendPost = function() {
var data = {
json: JSON.stringify({
id: $scope.newId,
name: $scope.newName
})
};
$http.post("http://localhost:8080/academi-co/resources/tags/languages/", data).success(function(data, status) {
$scope.hello = data;
if (response.data)
$scope.msg = "Post Data Submitted Successfully!";
}, function (response) {
$scope.msg = "Service not Exists";
$scope.statusval = response.status;
$scope.statustext = response.statusText;
$scope.headers = response.headers();
})
}
})
推荐阅读
- regex - 正则表达式匹配两个之间没有空格的字符串#
- c# - .rdlc 本地处理异常中的打印问题
- security - 通过 Jenkins 的 HTTP 主机中毒漏洞进行未经授权的 URL 重定向
- python-3.x - 如果任何实例处于活动状态,它在两种情况下都响应是
- ssl-certificate - 有没有办法在 Ubuntu 18 或更高版本中安装 pkcs12 (p12) 证书?
- sql - pyspark中的分组索引
- swift - iOS 14 MapKit 选择注释图像错误
- html - 点击链接后不清除查询参数
- javascript - 在 JavaScript 中是否有类似可选链接的赋值
- email - 电子邮件中的 MIME 多部分