doulu4413 2017-03-01 10:43
浏览 36
已采纳

angularjs没有返回php数据,尝试了一切

I am learning Angularjs and i am stuck with getting data from PHP file.

<head>
  <script>
    var app = angular.module("new",[]);
    app.controller("control",function($scope,$http){
      $http.get("index.php").then(function(response) {
        $scope.database = response.data;
      });
    });
  </script>
</head>
<body ng-app="new">
  <div ng-controller="control">
    <h1>{{database}}</h1>
  </div>
</body>

This the angular script i wrote. My php code is very simple

<?php
  header("Access-Control-Allow-Origin: *");
  header("Content-Type: application/json; charset=UTF-8");
  echo "How are you?";
?>

I am not able to get php data onto my html file using angularJS. I checked the code twice and nothing seem to be missing. I even tried json_encode(). Did i miss something?

  • 写回答

1条回答 默认 最新

  • dongshadu4498 2017-03-01 11:25
    关注

    Before all check you php file in right palace using console

    In you php code

    <?php
      header("Access-Control-Allow-Origin: *");
      header("Content-Type: application/json; charset=UTF-8");
      echo json_encode("How are you?");
      exit();
    ?>
    

    And in you controller

    $http.get("call.php").then(function(response) {
        $scope.database = response.data;
      });
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 乌班图ip地址配置及远程SSH
  • ¥15 怎么让点阵屏显示静态爱心,用keiluVision5写出让点阵屏显示静态爱心的代码,越快越好
  • ¥15 PSPICE制作一个加法器
  • ¥15 javaweb项目无法正常跳转
  • ¥15 VMBox虚拟机无法访问
  • ¥15 skd显示找不到头文件
  • ¥15 机器视觉中图片中长度与真实长度的关系
  • ¥15 fastreport table 怎么只让每页的最下面和最顶部有横线
  • ¥15 java 的protected权限 ,问题在注释里
  • ¥15 这个是哪里有问题啊?