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");
})

启动服务器后,页面上啥都没有,一篇空白。。。扎心了

阅读 3.1k
1 个回答

你的server端的信息写错了

推荐问题