REST вызов с POST не показывает ответное сообщение - PullRequest
0 голосов
/ 12 мая 2018

Я пытаюсь сделать POST-вызов с AngularJS и посмотреть, все ли работает, получив ответное сообщение, статус ответа и т. Д.

Что мне нужно, это посмотреть, все ли в порядке с 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>

1 Ответ

0 голосов
/ 12 мая 2018

Я изменил код на какой-то код, возможно попробуйте это.

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();
        })
    }
})
Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...