I am fairly new to angular and php. I am trying to make a post from an html page to a php page using angularjs. The post succeeds but response.data is the plain text of the php file instead of the json object I echo.
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);
?>
Does this help?
.then(function mySuccess(response){
let dataObj = JSON.parse(response.data);
$scope.last_name = dataObj.last_name;
...
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.