PHP json响应在角$ http中未定义

时间:2022-10-07 08:30:59

we are still learning how angular js works, we have little problem, we are making a http post request with angular

我们还在学习角度js是如何工作的,我们没有什么问题,我们正在制作带有角度的http post请求

var app = angular.module('app',['ngMessages']);
app.controller('AppCntrl', function($scope, PostingService) {
  $scope.submitData = function() {
    var data = {
      name: "abc",
      email: "email@example.com"
    };
    PostingService.postData(data);
  }
});
app.service('PostingService', ['$http',function($http) {
  this.postData = function(data) {
    $http.post(
      '/url/to/post/request',
      data
    )
      .then(function successCallback(response) {
      alert(response); // alerts undefined ***********************
    }, function errorCallback(response) {
      alert(response); // alerts undefined ***********************
    });
  }
}]);

while /url/to/post/request has output as header('Content-Type:application/json;'); and json response is like {"status":true,"message":"this is message"} but alert shows undefined

而/ url / to / post / request的输出为header('Content-Type:application / json;');和json响应就像{“status”:true,“message”:“这是消息”}但警报显示未定义

1 个解决方案

#1


0  

I've made few changes but it shouldn't matter. This code seems to work. > Change the variable requestURI

我做了一些改动但是没关系。这段代码似乎有效。 >更改变量requestURI

var app = angular.module('app', [])

app.controller('AppCtrl', function($scope, PostingService) {
  $scope.submitData = function() {
    var data = {
      name: 'abc',
      email: 'email@example.com'
    }
    PostingService.postData(data)
  }
})

app.service('PostingService', ['$http', function($http) {
  var requestURI = ''
  this.postData = function(data) {
    $http.post(requestURI, data)
      .then(function(response) {
        alert(response)
      }, function(response) {
        alert(response)
      })
  }
}])
<!DOCTYPE html>
<html>
  <head>
    <script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.2.23/angular.min.js"></script>
  </head>
  <body ng-app="app">
    <div ng-controller="AppCtrl">
      <button ng-click="submitData()">Make POST Request</button>
    </div>
  </body>

</html>

#1


0  

I've made few changes but it shouldn't matter. This code seems to work. > Change the variable requestURI

我做了一些改动但是没关系。这段代码似乎有效。 >更改变量requestURI

var app = angular.module('app', [])

app.controller('AppCtrl', function($scope, PostingService) {
  $scope.submitData = function() {
    var data = {
      name: 'abc',
      email: 'email@example.com'
    }
    PostingService.postData(data)
  }
})

app.service('PostingService', ['$http', function($http) {
  var requestURI = ''
  this.postData = function(data) {
    $http.post(requestURI, data)
      .then(function(response) {
        alert(response)
      }, function(response) {
        alert(response)
      })
  }
}])
<!DOCTYPE html>
<html>
  <head>
    <script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.2.23/angular.min.js"></script>
  </head>
  <body ng-app="app">
    <div ng-controller="AppCtrl">
      <button ng-click="submitData()">Make POST Request</button>
    </div>
  </body>

</html>