JQGrid with WCF Data Services (OData); loadBeforeSend not called on Edit mode; Cannot do setRequestHeader

故事扮演 提交于 2019-12-01 20:27:51

问题


I am having a bit of a problem with this fantastic jqgrid plugin and my attempt to use it with WCF Data Services (not really, but the very similar odata4j services). By the way, if anyone is thinking about using jqgrid with odata services, please send me a line, I found answers to difficult questions like, for example, how to configure the grid xmlreader to read an odata xml structure defeating the jquery namespace search issue ( hints:

include jquery.xmlns.js

.....
$.xmlns.m = "http://schemas.microsoft.com/ado/2007/08/dataservices/metadata";
$.xmlns.d = "http://schemas.microsoft.com/ado/2007/08/dataservices";
.....
var feedXmlReaderOptions = {
    root: "feed",
    row: "entry",
    repeatitems: false,
    id: "feed>entry>id"
};
....
    colModel: [
    {
        name: "clmNumKey", 
        index: "clmNumKey", 
        width: 150, 
        xmlmap: "d|clmNum",
        editable: true
    }
    ....
)

Anyway, the issue is that odata services need the method that addresses the edit operation (DELETE, PUT, MERGE) to be sent as a custom request header on a normal POST, rather than it being an http method. The reason for that seems to be that most firewalls do not allow PUT and DELETE http methods to pass, because that is how you can, for example, place new files on the server, as well as delete files, in case you can guess the valid path. Long story short...the loadBeforeSend event is not triggered for inline or form edit...I can see it being triggered on a full data request of the grid, but I only get the serializeEditData event triggered when I submit from the edit form. I am worried because I went into the jqgrid source files (grid.formedit.js, grid.inlineedit.js) and I couldn't get any hits with the beforesend keywords, only serializeeditdata is showing there. Am I missing something? Is there another way to set the headers I need on the xhr ajax object the grid uses? Is that xhr object exposed by the grid?

Below you have the code I have to handle the edit events...again, the loadBeforeSend is not triggered...

Thank you in advance, Serban

$.extend($.jgrid.edit, {
    closeAfterEdit: true,
    closeAfterAdd: true,
    ajaxEditOptions: {
        contentType: "application/json"
    },
    mtype: 'POST',
    loadBeforeSend: function(xhr)
    {
        xhr.setRequestHeader("X-HTTP-Method", "MERGE");
        return xhr;
    },        
    serializeEditData: function (data) {
        delete data.oper;
        return JSON.stringify(data);
    }
});                                                  

回答1:


There are no loadBeforeSend parameter which you can set by $.jgrid.edit. The values from $.jgrid.edit defines default options of editGridRow.

To specify the loadBeforeSend callback which should be used during the corresponding Ajax request you should use ajaxEditOptions instead and specify beforeSend (see $.ajax):

$.extend($.jgrid.edit, {
    closeAfterEdit: true,
    closeAfterAdd: true,
    ajaxEditOptions: {
        contentType: "application/json",
        beforeSend: function (jqXHR, settings) {
            jqXHR.setRequestHeader("X-HTTP-Method", "MERGE");
        }
    },
    //mtype: 'POST', - it's already default
    serializeEditData: function (data) {
        delete data.oper;
        return JSON.stringify(data);
    }
});


来源:https://stackoverflow.com/questions/10286780/jqgrid-with-wcf-data-services-odata-loadbeforesend-not-called-on-edit-mode-c

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!