问题描述
我正在使用最新版本的 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
但是我找不到设置内容类型的方法(例如,没有$ Type类型的选项,例如在$ .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" },
...
});
ajaxRowOptions
用于 grid.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
相同的方式进行设置.如果在editoptions
或searchoptions
中使用dataUrl
参数,则ajaxSelectOptions
的参数很有用.例如,我使用colModel
内的dataUrl
来定义类型为edittype: 'select'
的列. 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执行的请求的内容类型的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!