连接服务器时出错! POST请求失败

时间:2018-04-20 02:19:37

标签: javascript node.js ajax

我正在使用node.js,javascript和MongoDB制作webApp。我写了一个ajax调用,它应该发起一个POST请求并将数据传输到服务器,但我收到错误'连接到服务器时出错!'就是这样。我很难调试出错的地方。 :/

这是JS:

function submitDataToDB(){
    alert("HEY!");
        var URL = '/populateTables';
        var name = $('#workoutName').val();
        $.ajax({
            type: 'POST',
            url: URL,
            dataType: 'html',
            data: 
            {
                'name': name,
            },
            success: function(msg) {
                alert('success');
            },
            error: function(xhr, ajaxOptions, thrownError) {
                console.log(thrownError);
                alert('Error while connecting to the server!' + name);
            }
        });
    }

如果需要任何验证,这是节点代码:

app.get("/dbadd", function(req,res){
    res.render('dbadd');
});

app.post("/populateTables", function(req,res){
    console.log("reached populateTables")
    res.send("YO");
})

此外,有人可以回答以下问题吗?

  1. 有没有更好的方法来做我想做的事情?
  2. 在错误部分中传递xhr,ajaxOptions作为参数有什么用?如果我没有传递参数怎么办

0 个答案:

没有答案