$ http.get()返回html而不是json

时间:2016-02-27 02:36:10

标签: angularjs

在json文件上使用$ http.get(),但响应是我正在使用的页面的HTML代码。也就是说,console.log(响应)打印整个test.html文件。

main.js在nodejs上运行,它加载了test.html。 test.html然后应该加载test.json。 console.log(' here')打印并且$ scope.hello已设置并正确显示。

main.js

var http = require("http");
var fs = require('fs');

http.createServer(function (request, response) {
    var data = fs.readFile('test.html', function(err,data) {
        response.writeHead(200, {'Content-Type': 'text/html'});
        response.write(data);
        response.end();
    });
}).listen(8081);

的test.html

<!DOCTYPE html>
<html ng-app="App">
  <head>
    <script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.5.0/angular.min.js">
    </script>

    <script>
    angular.module('App', []).controller('Ctrl', 
        function($scope, $http) {
            console.log('here');
            $http.get('test.json').success(function(response) {
                console.log(response);
                $scope.hello = "hi guys";
            });
        }
    );
    </script>
  </head>

  <body>
    <div ng-controller="Ctrl">
    <p>{{hello}}</p>
    </div>
  </body>
</html>

test.json

  { "papers" :[
     {
       "authors":"Zubrin, Robert M.; Baker, David A.; Gwynne, Owen",
       "title":"Mars Direct: A Simple, Robust, And Cost Effective Architecture For The Space Exploration Initiative",
       "titleLink":"http://www.marspapers.org/papers/Zubrin_1991.pdf",
       "abstractName":"Abstract",
       "abstractLink":"http://www.marspapers.org/abstr/Zubrin_1991abstr.htm",     
       "year":"1991",
       "category":"MissionEngring",
       "publcation":""
     }
 ]}

1 个答案:

答案 0 :(得分:0)

您的服务器经过专门编程,无论请求的内容如何,​​都会返回test.html的内容。您需要实际检查request并提供相应的文件。