PHP的json响应在angular $http中是未定义的
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>
相关文章:
- Http服务工厂将未定义的返回到Angular中的控制器
- 从 Angular 数据工厂中的 HTTP POST 请求接收未定义的结果
- Vue资源$http未定义
- Angularjs自定义服务变量未被$http成功定义
- AngularJS HTTP 资源 - 响应函数未定义
- AngularJs $http变量未定义
- 使用 NodeJS 的请求模块进行 HTTP 身份验证,返回未定义的正文
- 未定义使用 HTTP GET 请求返回对象
- 为什么我收到错误“错误未定义'http..'不是一个函数”
- 从工厂返回控制器$http承诺时未定义
- 为什么我在此控制器中得到“未定义$http”
- 无法使用$http读取未定义的属性“成功”
- Meteor.http 方法在服务器上未定义
- 尽管注入了$http,但它在服务中是未定义的
- Angular HTTP GET请求在浏览器中工作时返回未定义
- AngularJS Factory$http无法读取属性'长度'的未定义
- 带有Ajax的JavaScript和HTTP请求输出未定义的显示2
- AngularJS http.jsonp回调未定义
- Angular 1.x ES6服务$http未定义
- Angular2单元测试Http MockBackend问题-无法读取mergeOptions中未定义的属性合并