javascript 使用ajax将数据字符串传递给控制器方法
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/18160974/
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
Pass Data string using ajax to controller method
提问by user1938460
I am creating a filter list. I have a several multi-select lists where the user can choose several options. The problem is I am using ajax to return the values and call a controller method which will return a Json data which will then be displayed on the website. But the problem is it doesn't work.
我正在创建一个过滤器列表。我有几个多选列表,用户可以在其中选择多个选项。问题是我正在使用 ajax 返回值并调用一个控制器方法,该方法将返回一个 Json 数据,然后该数据将显示在网站上。但问题是它不起作用。
Controller Method:
控制器方法:
[HttpPost]
public ActionResult FilterData(string a, string b, string c, string d){
//Do Something with the strings e.g. filter the data.
return Json(result, JsonRequestBehavior.AllowGet);
}
Ajax Method for getting filtered data:
Ajax 获取过滤数据的方法:
$(document).ready(function() {
$("#submitBTN").click(function() {
var ab = $('#selectedID1').val();
var bc = $('#selectedID2').val();
var cd = $('#selectedID3').val();
var de = $('#selectedID4').val();
$.ajax({
contentType: "application/json; charset=utf-8",
url:"/Controller/FilterData",
type: "POST",
data: JSON.stringify({a: ab, b: bc, c: cd, d:de }),
success: function(result){
}
error: function(){}
});
return false;
});
});
Using firebug I can see the right values are posted. And I know that if I manually select string a via the url link it get the correct filtered data. However, this data does not seem to get into the method and output all the data without being filtered using the postes values.
使用 firebug 我可以看到正确的值被发布。而且我知道,如果我通过 url 链接手动选择字符串 a,它将获得正确的过滤数据。但是,这些数据似乎没有进入方法并输出所有数据,而没有使用 postes 值进行过滤。
The problem is that the Data Posted is not being pciked up or sent to the method FilterData, so it returns all the data as it should do if there are no filter options selected. I would like to know how I can fix the code so I can get the data posted by ajax to send that data to method FilterData parameters e.g. string a, string b, string c and string d.
问题是发布的数据没有被提取或发送到方法 FilterData,所以它返回所有数据,如果没有选择过滤器选项,它应该这样做。我想知道如何修复代码,以便我可以获取 ajax 发布的数据,以将该数据发送到方法 FilterData 参数,例如字符串 a、字符串 b、字符串 c 和字符串 d。
回答by Nenad
Update:Now when you restated problem, here is the line that had issue (you were using JSON.stringify on data value):
更新:现在当你重述问题时,这里是有问题的行(你在数据值上使用 JSON.stringify):
data: { a: ab, b: bc, c: cd, d: de }
You were converting data into 1 JSON string, which is not what MVC expects.
您正在将数据转换为 1 个 JSON 字符串,这不是 MVC 所期望的。
"{"a":1,"b":2,"c":3,"d":4}"
Instead it expects key value pairs (normal post-back format):
相反,它需要键值对(正常的回传格式):
a=1&b=2&c=3&d=4
So now full example without typos:
所以现在没有错别字的完整示例:
$(document).ready(function () {
$("#submitBTN").click(function(){
var ab = $('#selectedID1').val();
var bc = $('#selectedID2').val();
var cd = $('#selectedID3').val();
var de = $('#selectedID4').val();
$.ajax({
url: '/MyController/FilterData',
type: 'POST',
data: { a: ab, b: bc, c: cd, d: de },
success: function(result) {
},
error: function() {
}
});
});
});
You don't need JsonRequestBehavior.AllowGet
if it's just POST request (although it doesn't break anything).
你不需要JsonRequestBehavior.AllowGet
,如果它只是POST请求(尽管它不会破坏任何东西)。
回答by Daniele
try with:
尝试:
url:'/Controller/FilterData',