javascript - 没有从 nodeJS 中的 post 方法获得对 html 页面的响应

标签 javascript html node.js

这是我的 nodeJS 代码:

app.post('/signup', function (req, res) {
            res.status('500').send('showAlert');    
}); 

这是我的 Html post 方法调用

$.post("/signup", userData, function(error){
        if(error.responseText == 'showAlert')
        {
            alert("User already exists");
        }
    });

为什么我没有收到任何警报?

最佳答案

Status code 500 is indicator of Internal Server Error which is handled in error handler of ajax.

jQuery.post( url [, data ] [, success ] [, dataType ] )

引用这个例子:

var jqxhr = $.post( "example.php", function() {
  alert( "success" );
})
  .done(function() {
    alert( "second success" );
  })
  .fail(function() {
    alert( "error" );
  })
  .always(function() {
    alert( "finished" );
});

更新的代码:

$.post("/signup", userData, function(data) {
  console.log(data);
}).fail(function(error) {
  if (error.responseText == 'showAlert') {
    alert("User already exists");
  }
});

关于javascript - 没有从 nodeJS 中的 post 方法获得对 html 页面的响应,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39761851/

相关文章:

node.js - Mocha 单元测试 Mongoose 模型

javascript - 使用 Node.js 读取和写入多个 ip

json - 如何将已解决的 promise 值传递给另一个函数?

javascript - 按名称模式搜索本地 javascript 变量

java - 如何在 Javascript 中访问模型属性

javascript - C# 和 Javascript 代码计算给出不同的结果

html - 如何使背景颜色覆盖我的整个 DIV 而不仅仅是其中的文本?

javascript - WebGL 中 Uncaught Error : SECURITY_ERR: DOM Exception 18 When applying textures with Three. js

html - 水平 div 树

html - CSS网格布局问题