AngularJS $ http返回html源代码

问题描述 投票:1回答:2

我使用以下代码来执行http请求。

<!DOCTYPE html>
<html>

<head>
  <script src="http://ajax.googleapis.com/ajax/libs/angularjs/1.4.8/angular.min.js"></script>
  <!--head_end-->
</head>

<body>

  <div class="company_profile" ng-app="company_profile" ng-controller="company_profileCtrl">
    <h2>&company_name</h2>
    <p><a href="&company_link" target="_blank" rel="nofollow">&company_link_text</a>
    </p>
    <p>{{companyProfile}}</p>
  </div>

  <!-- populate company_profile -->
  <script>
    var app = angular.module('company_profile', []);
    app.controller('company_profileCtrl', function($scope, $http) {
      $http.get("single-site.php").then(function(response) {
        $scope.companyProfile = response.data;
      });
    });
  </script>
</body>

</html>

但是代码返回它所在的html文档的源代码。

single-site.php文件存在并返回json数据。

谢谢你的帮助

编辑我看到了有这个问题的人的其他三个问题,但没有一个问题得到正确解决。

javascript html angularjs
2个回答
1
投票

你在调用中缺少$ http.get('/')。你确定你的URL吗?我们将REST API链接映射到其中,而不是页面名称。

像下面的代码。

$http({
  method: 'GET',
  url: '/someUrl'
}).then(function successCallback(response) {
    // this callback will be called asynchronously
    // when the response is available
  }, function errorCallback(response) {
    // called asynchronously if an error occurs
    // or server returns response with an error status.
  });

0
投票

在url字段中,您只需编写完整路径即可。例如https://sitename/index.php/RandomController/yourfunction/ 对我来说它有效!附:我使用codeigniter

© www.soinside.com 2019 - 2024. All rights reserved.