jqGrid afterSubmit

Die*_*mos 3 validation jquery json http jqgrid

我正在返回一个名为insertStatus的JSON值,我想从函数aftersubmit中得到它,因为它如下:

var addOptions = 

      { 
          closeOnEscape: true,
          width:500,
          url: 'addMember', 
          savekey: [true, 13],
          afterSubmit : function(response, postdata) 
          { 
              alert(response.insertStatus);
          },
          resize : false,
          closeAfterAdd: true
      };
Run Code Online (Sandbox Code Playgroud)

但我只会显示消息"未定义".

我试图将InsertStatus的值作为JSON获取,因为此值将告诉我新记录的插入是否已成功保存到数据库.如果我不能从这里获得JSON值,也许我应该遵循另一种方法?

我之前使用errorText进行另一项任务,而不是返回JSON值,而是返回了一个带有自定义错误消息的HTTP错误状态.这将是最好的方法吗?即使第二个方法更好,我也很想知道第一个问题的答案.谢谢你的帮助.

Ole*_*leg 6

jqGrid的表单编辑模块使用complete回调jQuery.ajax而不是典型的success回调(参见源代码).所以afterSubmit回调的第一个参数(response参数)是对象,它将在jqGrid文档中命名为jqXHR.它的延伸XMLHttpRequest.因此,您应该使用responseTextproperty来访问服务器的普通响应.如果服务器返回带有insertStatus编码为JSON字符串的对象,则必须先解析JSON字符串response.responseText,然后再获取insertStatus属性.相应的代码afterSubmit可以是以下内容:

afterSubmit: function (response, postdata) {
    var res = $.parseJSON(response.responseText);
    if (res && res.insertStatus) {
        alert(res.insertStatus);
    }
    // you should don't forget to return
    //     return [true, ""];
    // in case of successful editing and return
    //     return [true, "", newId];
    // with the Id of new row generated from the server
    // if you would use reloadAfterSubmit: false
    // option of editGridRow
}
Run Code Online (Sandbox Code Playgroud)