【问题标题】:Send a success response to POST request向 POST 请求发送成功响应
【发布时间】:2017-03-01 22:17:57
【问题描述】:

我有以下 ajax 调用:

function sendRequest(quote, author){
    $.ajax({
        type: "POST",
        url: window.location.pathname,
        data: {quote: quote, author: author},
        dataType: JSON,
        success: function(){console.log("Data sent!");},
        error: function(error){console.log("ran into an error")}
    });
}

这是我的服务器(使用快递)处理发布请求,

app.post("/", function(req, res){res.status(200).end("Success!")});

但是,控制台不打印“Data Sent”。相反,它会打印“ran into an error

其他一切都经过测试并且工作正常

【问题讨论】:

  • 尝试像这样解决错误:console.log("ran into an error :: ", error);该错误可能会帮助您找到问题。
  • dataType: JSON, - 应该是dataType: "JSON",
  • 您是否忘记在提交功能底部输入return false;(如果您使用的是提交按钮)?
  • @PHPglue,不,我做到了

标签: javascript ajax express routing


【解决方案1】:

首先dataType应该是'json'而不是JSON

然后检查控制台日志以了解具体错误。

【讨论】:

    猜你喜欢
    • 2020-09-30
    • 1970-01-01
    • 2020-07-29
    • 1970-01-01
    • 2021-05-29
    • 2014-08-04
    • 1970-01-01
    • 2017-10-12
    • 2018-09-16
    相关资源
    最近更新 更多