AJAX邮政的JavaScript字符串数组JsonResult的名单,其中,串&GT;总是返回NULL? [英] AJAX Post of JavaScript String Array to JsonResult as List<string> Always Returns Null?
问题描述
我试图建立一个字符串数组中的JavaScript和得到的结果在一个字符串列表中的操作方法。下面是我的JavaScript的样子。我使用jQuery 1.4.2。问题是我的操作方法列表总是显示NULL。将一个JavaScript字符串数组没有正确映射到一个字符串列表在C#?
VAR测试= ['的test1,test2的'];
VAR PARMS = {
VAR1:一些字符串,
VAR2:测试
};
$阿贾克斯({
键入:POST,
网址:/测试/ JSONTestAction
异步:假的,
数据:PARMS,
数据类型:JSON,
成功:功能(数据){
// 成功
}
});
然后我JsonResult如下所示:
公共JsonResult JSONTestAction(字符串VAR1,列表和LT;字符串&GT; VAR2)
{
// VAR2总是空 - 不是很好
返回JSON(新{测试=测试});
}
我更新到1.4.2的jQuery后面临同样的问题。你可以在这里找到解决办法 (在阿贾克斯一节)。
添加传统:在阿贾克斯选择真正应该
$。阿贾克斯({
键入:POST,
传统:真正的,
网址:/测试/ JSONTestAction
异步:假的,
数据:PARMS,
数据类型:JSON,
成功:功能(数据){
// 成功
}
});
I'm trying to build up a string array in JavaScript and get the results in a string list in the action method. Below is what my JavaScript looks like. I'm using jQuery 1.4.2. The problem is my List in the action method is always showing NULL. Will a JavaScript string array not map correct to a string list in C#?
var test = ['test1', 'test2'];
var parms = {
var1: 'some string',
var2: test
};
$.ajax({
type: "POST",
url: "/Test/JSONTestAction",
async: false,
data: parms,
dataType: "json",
success: function(data) {
// success
}
});
Then my JsonResult looks like the following:
public JsonResult JSONTestAction(string var1, List<string> var2)
{
// var2 is always NULL -- not good
return Json(new { test = "test" });
}
I faced the same problem after updating to jquery 1.4.2. You can find the solution here (in the Ajax section).
Adding traditional : true in the ajax options should work.
$.ajax({
type: "POST",
traditional: true,
url: "/Test/JSONTestAction",
async: false,
data: parms,
dataType: "json",
success: function(data) {
// success
}
});
这篇关于AJAX邮政的JavaScript字符串数组JsonResult的名单,其中,串&GT;总是返回NULL?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!