繁体   English   中英

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

[英]PHP json response is undefined in angular $http

我们仍在学习angular js的工作方式,没什么问题,我们正在用angular发出一个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 ***********************
    });
  }
}]);

/url/to/post/request已输出为header('Content-Type:application/json;'); 和json响应类似于{"status":true,"message":"this is message"}但警报显示未定义

我所做的更改很少,但是没关系。 此代码似乎有效。 >更改变量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> 

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM