![](/img/trans.png)
[英]AngularJS: Why is $http response.data an object, when php returns a JSON string?
[英]AngularJS HTTP Post response.data is Raw Text of PHP
我对angular和php很陌生。 我正在尝试使用angularjs从html页面发布到php页面。 帖子成功,但是response.data是php文件的纯文本,而不是我回显的json对象。
的test.html
<!DOCTYPE html>
<html>
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.4.8/angular.min.js"></script>
<body>
<div ng-app="myApp" ng-controller="myCtrl" ng-init="LoadPage('kengoss@yahoo.com')">
<p>Welcome Dr. {{last_name}}</p>
</div>
<script>
var app = angular.module('myApp', []);
app.controller('myCtrl', function($scope, $http)
{
$scope.last_name = 'Summers';
$scope.LoadPage = function($email)
{
$scope.last_name = $email;
$http({
method: "post",
url: "test.php",
data: { email: $email},
headers: { 'Content-Type': 'application/x-www-form-urlencoded'}
}).then(function mySuccess(response){
$scope.last_name = response.data;
},function myFailure(response){
$scope.last_name = 'Failure';
}
);
}
});
</script>
</body>
</html>
test.php的
<?php
header('Content-Type: application/json');
$array['last_name'] = 'Goss';
echo json_encode($array);
?>
这有帮助吗?
.then(function mySuccess(response){
let dataObj = JSON.parse(response.data);
$scope.last_name = dataObj.last_name;
...
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.