首页 > 解决方案 > 带有 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>

标签: htmlangularjsrest

解决方案


我将代码更改为一些代码,也许试试这个。

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

推荐阅读