来自本地httpd(节点)的jQuery .load()

时间:2013-01-09 19:16:28

标签: node.js jquery

Node.js服务器

var http = require("http");
http.createServer(function(request,response){
    console.log("client connected");
    response.writeHeader(200,{"Content-type": "text/html"}); 
    response.write("Hello ;-)");
    response.end();
}).listen(9090);

我尝试将服务器输出加载到ID为“myDisplayField”的div中:

$("#myDisplayField").load("http://localhost:9090");

(HTML-File位于我当地的httpd上)

我遇到的问题是,节点服务器可能会显示他从ajax(“客户端已连接”)获得请求,但“Hello ;-)”/网站内容未加载到# myDisplayField as expteced。

如果我建立一个像Apache这样的网络服务器,并在htdocs中放入一个带有“Hello ;-)”的index.html,整个过程就可以了。

我做错了什么?

1 个答案:

答案 0 :(得分:0)

我刚尝试了你的代码,并导航到localhost:9090按预期显示'hello'正文。

可能是jquery的load()方法查找其他数据 - 例如,标题中的内容类型 - 您的非常简单的响应无法提供。您应该使用更高级别的服务器,例如express:

,而不是单独构建每个http响应
var express = require('express');
var app = express();

app.use(function(req, res, next) {
  res.send('Hello');
});

app.listen(9090);