jqGrid afterSubmit

11,968

Form editing module of jqGrid uses complete callback of jQuery.ajax instead of typical success callback (see the source code). So the first parameter of afterSubmit callback (the response parameter) is object which will be named in jqGrid documentation as jqXHR. It's extension of XMLHttpRequest. So you should use responseText property to access the plain response from the server. If the server return object with insertStatus encoded as JSON string then you have to parse JSON string response.responseText first and only then get insertStatus property. The corresponding code of afterSubmit can be about the following:

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
}
Share:
11,968
Diego Ramos
Author by

Diego Ramos

Updated on June 04, 2022

Comments

  • Diego Ramos
    Diego Ramos almost 2 years

    I am returning a JSON value called insertStatus and I'd like to get it from the function aftersubmit which I have as it follows:

    var addOptions = 
          
          { 
              closeOnEscape: true,
              width:500,
              url: 'addMember', 
              savekey: [true, 13],
              afterSubmit : function(response, postdata) 
              { 
                  alert(response.insertStatus);
              },
              resize : false,
              closeAfterAdd: true
          };
      
    

    But I will just display the message "undefined".

    I am trying to get the value of InsertStatus as JSON because this value will tell me if the insert of the new record was successfully saved to the Database or not. Maybe I should follow another approach if I cannot get the JSON value from here?

    I used errorText before for another task and instead of returning a JSON value I returned an HTTP error status with a custom error message. Which would be the best approach? Even if the second approach was better I'd really like to know the answer of the first one. Thanks for your help.