【发布时间】:2014-10-06 20:38:54
【问题描述】:
我有一个使用 jQuery 的 html 页面。
我想向节点服务器发送请求,但我不知道如何响应不同的请求,换句话说,如何区分 get\post 请求以及如何读取请求正文(取消用户想要的内容)和响应根据它。
var http = require("http");
function serve(request, response)
{
response.writeHead(200, {"Content-Type": "text/plain"});
}
http.createServer(serve).listen(3001);
在客户端,同样的问题 - 如何发送数据?
Window.onload = function () {
Document.getElementById('GoButton').click = function() {
var xhr = new XHRObject();
xhr.open("get","http://127.0.0.1:1337",true);
xhr.onreadystatechange= function()
{
if (xhr.readyState == 4 && xhr.status == 200)
{
document.getElementById("myDiv").innerHTML=xmlhttp.responseText;
}
};
request.send(null);
};
};
最后一件事:所有这些请求和响应都应该使用 json。
【问题讨论】:
标签: javascript html json node.js http