【问题标题】:why success function is never called and how can i fix it?为什么从不调用成功函数,我该如何解决?
【发布时间】:2018-04-21 23:11:57
【问题描述】:

当我尝试发帖时,数据被发送并且服务器接收它但从未调用成功函数。连接在 chrome 的网络检查器选项卡中显示为已停止,并显示警告:连接尚未完成。

脚本:

var modif = {
    CRN: $("#DIAS").parent().parent().parent().children()[0].children[0].innerText,
    DIAS: $("#DIAS").val(),
    start_hr: $("#STRHR").val(),
    end_hr: $("#ENDHR").val(),
    title: $("#TITLE").val()
}
$.ajax({
    url: '/cmanager/edit',   
    dataType: 'text',
    type: 'POST',
    data: modif,
    success: function (order) {
        alert("functiono!");
    },
    error:   function(jqXHR, textStatus, errorThrown) {
    alert("Error, status = " + textStatus + ", " +
          "error thrown: " + errorThrown
    );

服务器:

app.post("/cmanager/edit",function (req, res) {
var CRN = req.body.CRN;
var DIAS = req.body.DIAS;
var start_hr = req.body.start_hr;
var end_hr = req.body.end_hr;
var title = req.body.title;
console.log(CRN);
console.log(DIAS);
console.log(start_hr);
console.log(end_hr);
console.log(title);
var usrq = "update section natural join class set DIAS = '"+DIAS+"', start_hr = '"+start_hr+"', end_hr = '"+end_hr+"', title = '"+title+"' where CRN = '"+CRN+"';";
  connection.query(usrq, function (error, results, fields) {
    if (error) 
    console.log(error.code);
    else
    try {
        console.log("hello");
    } catch (error) {
        console.log("bad ifo by client");
    }
  });
})

【问题讨论】:

  • 你有一个错字。应该是success 而不是succes
  • @Li357 这是一件小事,但将其作为答案发布,以便问题可以结束。
  • 谢谢,抱歉。还是不行。

标签: javascript jquery ajax express post


【解决方案1】:

您实际上在服务器中的任何地方都没有将任何数据发送回客户端。

在您的服务器中,您至少需要调用res.end()。在我看来你真正想要的是res.sendStatus(204);

还请注意,您对 SQL 注入攻击非常开放,如果您还没有被黑客入侵,那么您将被黑客入侵。始终使用参数化查询来完全避免这个问题。

【讨论】:

  • 谢谢!肯定会照顾 SQL 安全性。
猜你喜欢
  • 1970-01-01
  • 2020-09-30
  • 2021-10-26
  • 1970-01-01
  • 2020-10-22
  • 1970-01-01
  • 2019-05-26
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多