简体   繁体   English

AngularJS HTTP Post response.data是PHP的原始文本

[英]AngularJS HTTP Post response.data is Raw Text of PHP

I am fairly new to angular and php. 我对angular和php很陌生。 I am trying to make a post from an html page to a php page using angularjs. 我正在尝试使用angularjs从html页面发布到php页面。 The post succeeds but response.data is the plain text of the php file instead of the json object I echo. 帖子成功,但是response.data是php文件的纯文本,而不是我回显的json对象。

test.html 的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 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;
  ...

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

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