JQGrid with WCF Data Services (OData); loadBeforeS

2019-02-24 06:42发布

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条回答
时光不老,我们不散
2楼-- · 2019-02-24 07:18

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);
    }
});
查看更多
登录 后发表回答