简体   繁体   English

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

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

we are still learning how angular js works, we have little problem, we are making a http post request with angular 我们仍在学习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 ***********************
    });
  }
}]);

while /url/to/post/request has output as header('Content-Type:application/json;'); /url/to/post/request已输出为header('Content-Type:application/json;'); and json response is like {"status":true,"message":"this is message"} but alert shows undefined 和json响应类似于{"status":true,"message":"this is message"}但警报显示未定义

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> 

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

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