服务端代码是:
var http=require('http');
var querystring=require('querystring');
var util=require('util');
http.createServer(function(req, res) {
var postData = "";
req.on("data", function (data) {
postData += data;
});
req.on("end", function () {
post = querystring.parse(postData);
res.write(post);
res.end();
});
}).listen(3000, function() {
console.log("Starting to listen on port 3000");
})
ajax的代码是:
$.ajax({
type: "POST",
url: "http://localhost:3000",
dataType:"jsonp",
data: {
user: $username,
pwd: $password
},
success: function(data) {
var data=JSON.parse(data);
console.log(data);
},
error: function() {
alert("出错啦!");
}
})
ajax发送的是两个表单数据,打开localhost:3000/index.html是空白的,输入框都没有,控制台也没有,也不报错。。。
你的index.html 页面呢??
在服务器,你没有处理请求 ,
你连请求的url 都没有解析,
localhost:3000/index.html 是一个get 请求 ,你就应该解析一下url判断是这个路径的时候,把一个文件fs.readFile(path),获取到数据写入到res.write 里面
记得要有res.end(),不然请求会挂起,页面就是一直转圈圈.
post请求同理,如果不是通过服务器返回的页面,post 请求还有一个跨域问题,
这个问题太多了.
直接照下面的敲吧,如果不懂服务器的基础知识,我只能表示很绝望
http://www.runoob.com/nodejs/node-js-get-post.html