$ http.get()返回html而不是json
在一个json文件上使用$ http.get(),但是响应是我正在使用的页面的HTML代码。也就是说,console.log(response)打印整个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":""
}
]}
你的服务器是专门编程返回的test.html
不管内容是什么要求。您需要实际检查request
并提供相应的文件。
我不关注。是的,节点将返回test.html的内容,但我会认为当我加载页面角部分将运行并加载json文件。 – joshbaldwin42
@ joshbaldwin42 Angular只能加载你服务的数据,不管你从节点请求什么,你都有特定的节点来返回'test.html'的内容。你可以去'http:// localhost:8081/iiwugeiugifugiugwygfiugriqgriuqgr',你会得到'test.html'的内容。你可以去'http:// localhost:8081/test.json',你会得到'test.html'的内容。不管你要求什么URL,你都有特定的节点来**忽略请求**并提供'test.html'的内容。 – meagar
我想我明白了。我假设$ http.get()是在角度上完成的,但我需要在$ http.createServer()函数中添加此功能来处理此请求。听起来像我需要更多地查看节点。谢谢! – joshbaldwin42
它发布了什么html ..可能是一些错误编码,它认为更新您的文章,以获得一些快速帮助.. – Prasad
一个更多的事情在你的服务器端编码你需要发送response.writeHead(200, { '内容 - 类型': '应用/ JSON'});而不是text/html – Prasad
而不是发送test.html你需要查询你的数据库,然后在json中返回响应然后它的作品! – Prasad