设置 jQuery jqGrid 执行的请求的内容类型
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/2675625/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Setting the content-type of requests performed by jQuery jqGrid
提问by Nigel
I am using the latest version of jqGrid: 3.6.4
我正在使用最新版本的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
and I would like it to be:
我希望它是:
application/json; charset=utf-8
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).
但我找不到设置内容类型的方法(例如,您在 $.ajax 调用中找不到 contentType 选项)。
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.
所以只是为了澄清,我不是在问如何在 jQuery 服务器请求上设置内容类型,而是特别使用 jqGrid,它没有提供明显的选项来执行此操作。
Thanks, Nigel.
谢谢,奈杰尔。
Update:Oleg's response fixed solved it.
更新:Oleg 的回复解决了这个问题。
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',
...
回答by Oleg
How you can find in the code of grid.base.jsthe $.ajax
call filling the grid contain looks like following:
如何在grid.base.js的代码中找到$.ajax
填充网格的调用包含如下所示:
$.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));
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
所以你可以使用ajaxGridOptions
jqGrid 的选项来设置或覆盖$.ajax
请求的任何参数。因为我只对我的服务器使用 JSON 请求,所以我设置了一般设置contentType
like
$.extend($.jgrid.defaults, {
datatype: 'json',
{ajaxGridOptions: { contentType: "application/json" },
{ajaxRowOptions: { contentType: "application/json", type: "PUT" },
...
});
The ajaxRowOptions
are used in grid.inlinedit.jsfor row editing. For the form edit there are other parameters, which I set also as global setting:
将ajaxRowOptions
主要应用在grid.inlinedit.js用于行编辑。对于表单编辑,还有其他参数,我也将其设置为全局设置:
$.extend($.jgrid.edit, {
ajaxEditOptions: { contentType: "application/json" },
...
});
$.extend($.jgrid.del, {
ajaxDelOptions: { contentType: "application/json" },
mtype: "DELETE",
...
});
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()
.
您如何看到我的服务器是一个 RESTfull 服务(主要在 WFC 中开发,其余在 ASP.NET MVC 中开发)。因为$.jgrid.edit
是“添加”和“修改”项目的设置,我不能只更改mtype: "PUT"
“编辑”,所以我在navGrid()
.
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.
您会发现最后一个 ajax 参数也很有趣,它是ajaxSelectOptions
. 您可以按照与ajaxGridOptions
. ajaxSelectOptions
如果dataUrl
在editoptions
或 中使用参数,则 的参数很有用searchoptions
。例如,我使用dataUrl
inside ofcolModel
来定义类型的列edittype: 'select'
。选择选项的可能值将从服务器加载以进行内联或表单编辑或在搜索对话框内。因为对于这样的数据加载都是使用ajax,所以有相应的ajaxSelectOptions
选项。
Best regards.
此致。