设置由 jQuery jqGrid 执行的请求的内容类型 [英] Setting the content-type of requests performed by jQuery jqGrid

查看:23
本文介绍了设置由 jQuery jqGrid 执行的请求的内容类型的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用最新版本的 jqGrid:3.6.4

I am using the latest version of jqGrid: 3.6.4

这似乎是一个简单的问题(或者至少在我花了几个小时之前它是这样的):

This seems like a simple problem (or at least it did before I spent a few hours on it):

当网格向服务器(向控制器操作)发送请求时,其内容类型始终为:

When the grid sends a request to the server (to a controller action), its content-type is always:

application/x-www-form-urlencoded; charset=UTF-8

我希望它是:

application/json; charset=utf-8

但我找不到设置内容类型的方法(没有 contentType 选项,例如 $.ajax 调用).

but I can find no way of setting the content-type (there is no contentType option as you would find on a $.ajax call for example).

所以澄清一下,我不是在问如何在 jQuery 服务器请求上设置内容类型,而是专门使用 jqGrid,它没有提供明显的选项.

So just to clarify, I am not asking how to set the content-type on a jQuery server request, but specifically using jqGrid, which does not provide an obvious option for doing this.

谢谢,奈杰尔.

更新:奥列格的回应解决了它.

Update: Oleg's response fixed solved it.

以下是网格的选项设置:

Here are the option settings for the grid:

jQuery("#ContactGridList").jqGrid({
        url: '/ContactSelect/GridData/',
        datatype: 'json',
        ajaxGridOptions: { contentType: 'application/json; charset=utf-8' },
        mtype: 'POST',
        ...

推荐答案

如何在grid.base.js的代码中找到$.ajax调用填充网格包含如下所示:

How you can find in the code of grid.base.js the $.ajax call filling the grid contain looks like following:

$.ajax($.extend({
    url: ts.p.url,
    type: ts.p.mtype,
    dataType: dt,
    data: $.isFunction(ts.p.serializeGridData) ?
             ts.p.serializeGridData.call(ts, ts.p.postData) : ts.p.postData,
    complete: function (req, st) {
       ...
    }
    ...
}, $.jgrid.ajaxOptions, ts.p.ajaxGridOptions));

因此您可以使用 jqGrid 的 ajaxGridOptions 选项来设置或覆盖 $.ajax 请求的任何参数.因为我只对服务器使用 JSON 请求,所以我设置了 contentType 的常规设置,例如

So you can use ajaxGridOptions option of jqGrid to set or override any parameter of $.ajax request. Because I use only JSON requests to my server, I set general setting of contentType like

$.extend($.jgrid.defaults, {
    datatype: 'json',
    {ajaxGridOptions: { contentType: "application/json" },
    {ajaxRowOptions: { contentType: "application/json", type: "PUT" },
    ...
});

ajaxRowOptionsgrid.inlinedit.js 中用于行编辑.对于表单编辑还有其他参数,我也将其设置为全局设置:

The ajaxRowOptions are used in grid.inlinedit.js for row editing. For the form edit there are other parameters, which I set also as global setting:

$.extend($.jgrid.edit, {
    ajaxEditOptions: { contentType: "application/json" },
    ...
});

$.extend($.jgrid.del, {
    ajaxDelOptions: { contentType: "application/json" },
    mtype: "DELETE",
    ...
});

您如何看到我的服务器是一个 RESTfull 服务(主要在 WFC 中开发,其余在 ASP.NET MVC 中开发).因为 $.jgrid.edit 是添加"和修改"项目的设置,我不能只为编辑"更改 mtype: "PUT",所以我在 navGrid() 的参数中执行此操作.

How you can see my server is a RESTfull service (developed mainly in WFC and the rest in ASP.NET MVC). Because $.jgrid.edit is a setting for both "add" and "modify" items, I could not change mtype: "PUT" for "edit" only, so I do this in parameters of navGrid().

您可能会发现设置的最后一个 ajax 参数是 ajaxSelectOptions.您可以按照与 ajaxGridOptions 相同的方式设置它.如果您在 editoptionssearchoptions 中使用 dataUrl 参数,则 ajaxSelectOptions 的参数很有用.例如,我使用 colModel 内部的 dataUrl 来定义 edittype: 'select' 类型的列.选择选项的可能值将从服务器加载以进行内联或表单编辑或在搜索对话框内.因为这样的数据加载都使用了ajax,所以有对应的ajaxSelectOptions选项.

The last ajax parameter which you could find also interesting to set is ajaxSelectOptions. You can set it on the same way as ajaxGridOptions. Parameters of ajaxSelectOptions are useful if you use dataUrl parameter inside of editoptions or searchoptions. I use, for example, dataUrl inside of colModel for defining columns of the type edittype: 'select'. The possible values of select option will be loaded from server for inline or form editing or inside of search dialog. Because for such data loading are used ajax, there is corresponding ajaxSelectOptions option.

最好的问候.

这篇关于设置由 jQuery jqGrid 执行的请求的内容类型的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆