javascript - ajax请求nodejs后台,开启服务器后,localhost:3000/index.html页面既没有报错,也没有文字。。。
本文介绍了javascript - ajax请求nodejs后台,开启服务器后,localhost:3000/index.html页面既没有报错,也没有文字。。。的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
问 题
index.html的代码为(页面上有两个输入框和一个按钮):
$("input[type=button]").on("click",function() {
if ( $username=="" || $password=="" ) {
alert("请输入完整!");
} else {
$.ajax({
type: "POST",
url: "http://localhost:3000",
data: {
user: $username,
pwd: $password
},
success: function(data) {
var data=JSON.parse(data);
console.log(data);
},
error: function() {
alert("出错啦!");
}
})
}
})
server.js的代码是:
var http=require('http');
var querystring=require('querystring');
http.createServer(function(req, res) {
var data="";
req.on("data", function(chunk) {
data+=chunk;
})
req.on("end", function() {
data=querystring.stringify(data);
res.end(data);
})
}).listen(3000, function() {
console.log("Starting to listen on port 3000");
})
启动服务器后,页面上啥都没有,一篇空白。。。扎心了
这个问题已被关闭,原因:问题已解决 - 问题已解决,且对他人无借鉴意义
解决方案
你的server端的信息写错了
理解一下webserver的原理
你看一下这个简单的例子,看看知不知道怎么改
https://github.com/cristicmf/...
这篇关于javascript - ajax请求nodejs后台,开启服务器后,localhost:3000/index.html页面既没有报错,也没有文字。。。的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文