【问题标题】:Why $.ajax() call back function are never called?为什么 $.ajax() 回调函数永远不会被调用?
【发布时间】:2014-12-15 22:31:19
【问题描述】:

我有一个 Web 应用程序,它有许多按钮调用代码隐藏页面以执行 SQL 中的某些函数,然后向用户返回一条消息..

所有按钮都按预期工作。除了我要描述的那个。 请注意,我从另一个按钮单击事件中复制了相同的 javascript 代码。

点击按钮时会发生以下情况:

a) click 事件被触发。

b) $.ajax() 函数之前的任何代码都可以正常工作

c) 调用函数后面的代码,并按预期工作

d) 后面代码的返回值没问题。

e) $.ajax() 中的成功/错误函数不会被触发。

为什么?我在这里做错了什么?

我有以下 HTML:

<div class='button approve' req_id='123' user_id='ahmad' reason='something'>
    Approve
</div>

还有下面的javascript:

$(document).on('click', '.button.approve', function () {
  var req_id = $(this).attr('req_id');
  var reason = $(this).attr('reason');
  var user_id = $(this).attr('reason');

  alert('Before ajax');  //this is triggered normally.

  $.ajax({
        type: "POST",
        url: "getData.aspx/approve",
        data: "{req_id:'" + req_id + "', reason:'" + reason + "',user_id:'" + user_id + "'}",
        contentType: "application/json; charset=utf-8",
        dataType: "json",
        succss: function (data) {
            alert('success');    //this never gets called
        },
        error: function (a, b, c) {
            alert('error');      //this never gets called
        }
    });
});

以及后面的 ASPX 代码:

<WebMethod()> Public Shared Function approve(ByVal req_id As String, ByVal user_id As String, ByVal reason As String) As String
        Dim reply As String = "-received data is " & req_id & user_id & reason

        Return reply   'in debug mode, the value of reply as valid
    End Function

【问题讨论】:

  • 您手动构建 json 数据字符串的任何原因?那是非常危险的。为什么要构建 json-in-javascript?为什么不直接构建一个简单的 Javascript 对象并直接传递呢?照原样,您很容易创建 JS 语法错误
  • @MarcB 您能否详细说明您的建议?一个链接也可以。高级感谢
  • data: {req_id:req_id, reason:reason:user_id:user_id}。让 javascript 和 jquery 处理细节。如果你在 json 字符串中嵌入文本,你最终会做reason:'Miles O'Brien',并且繁荣,语法错误 - 相当于 sql 注入攻击的 javascript。
  • 在调用 ajax 之前,我已经用文本中的其他内容替换了任何分号和双引号。我会试试你的方法..
  • @Ahmad 手动序列化事情几乎肯定会变成一团糟。让语言为您完成工作,听从 Marc B 的建议。

标签: javascript jquery html asp.net ajax


【解决方案1】:

你有一个拼写错误。你写的是succss,应该是success

【讨论】:

    【解决方案2】:

    错字:succss 应该是 success

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2018-11-12
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-02-18
      • 1970-01-01
      相关资源
      最近更新 更多