search

Home  >  Q&A  >  body text

javascript - Ajax requests the nodejs background. After starting the server, the localhost:3000/index.html page neither reports an error nor has text. . .

The code of

index.html is (there are two input boxes and a button on the page):

$("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("出错啦!");
                }
            })
        }
    })

The code of server.js is:

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

After starting the server, there is nothing on the page and the article is blank. . . Heartbreaking

某草草某草草2745 days ago759

reply all(1)I'll reply

  • 迷茫

    迷茫2017-05-24 11:40:49

    The information on your server side is wrong

    • Understand the principles of webserver

    • Take a look at this simple example and see if you know how to change it
      https://github.com/cristismf/...

    reply
    0
  • Cancelreply