【问题标题】:ajax call is never post back to the serverajax 调用永远不会回传到服务器
【发布时间】:2015-10-03 17:27:35
【问题描述】:

我正在尝试对服务器进行 ajax 回调以将新数据保存到数据库中: 这是我的代码:

$.ajax({
    type: 'POST',
    url: '@Url.Action("SaveNewApplication")',
    contentType: 'application/json; charset=utf-8',
    data: {
        appName: newAppName,
        appDesc: newAppDesc,
        expire: newDaysToExpire,
        displayNote: newDspNote,
        adminRole: newAdminRole,
        defualtRole: newDefaultRole,
        active: newIsActive
    },
    dataType: '',
    success: function () {
        alert("new data has been saved");
    },
    error: function () {
        alert("Error happened whiles saving the new application data");
    }
});

我在“SaveNewApplication”函数处设置了一个断点,但断点从未命中! 这里是“SaveNewApplication”函数:

    [HttpPost]
    [OutputCache(Duration = 0)]
    public void SaveNewApplication(string appName, string appDesc, int expire, string displayNote, string adminRole,
       string defualtRole, bool active)
    {

    }

ajax调用中的成功函数永远不会执行

【问题讨论】:

  • 您的 contentType 错误,或者您的数据以错误的格式发送。 jQuery 不会自动将您的对象转换为 json,如果您想使用 JSON.stringify(thedata) 发布 JSON,则必须自己执行此操作
  • 你检查过firebug中的任何错误吗?

标签: javascript jquery asp.net ajax asp.net-mvc-3


【解决方案1】:

我发现了问题所在,传递的参数之一为空,所以调用永远不会返回服务器。

【讨论】:

    【解决方案2】:

    如果您将 contentType 指定为 json,请确保您实际发送的是 JSON。 JSON.stringify() converts javascript values into JSON strings

    $.ajax({
        type: 'POST',
        url: '@Url.Action("SaveNewApplication")',
        contentType: 'application/json; charset=utf-8',
        data: JSON.stringify({
            appName: newAppName,
            appDesc: newAppDesc,
            expire: newDaysToExpire,
            displayNote: newDspNote,
            adminRole: newAdminRole,
            defualtRole: newDefaultRole,
            active: newIsActive
        }),
        dataType: '',
        success: function () {
            alert("new data has been saved");
        },
        error: function () {
            alert("Error happened whiles saving the new application data");
        }
    });
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-11-06
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多