Ajax调用使用jQuery在ASP.NET MVC不传递参数 [英] Ajax call with jQuery in ASP.NET MVC does not pass parameters
问题描述
这条路线是:
routes.MapRoute(
阿贾克斯,//路线名称
的BizTalk /服务/ {行动},// URL与参数
新
{//参数的默认值
控制器=的BizTalk
}
);
我的控制器是:
公共JsonResult AjaxTest(字符串s,诠释我,布尔B)
{
返回JSON(S:+ S +,+我:+ I +,+B:+ b)的;
}
我的jQuery的code:
$(文件)。就绪(函数(){
$(#btn_test)。点击(函数(){
VAR S =测试;
VAR I = 8;
变种B =真实;
$阿贾克斯({
键入:POST,缓存:假的,
网址:/的BizTalk /服务/ AjaxTest
数据:{I:I,S:S,B:B},
的contentType:应用/ JSON的;字符集= UTF-8,
数据类型:JSON,
成功:函数(MSG){
}
});
});
});
这职位说明问题和可能的溶液(类似于如何@Erv已解释)。
如果您删除的contentType:应用/ JSON的;字符集= UTF-8
从您的来电 jQuery.ajax
默认的内容类型(表单urlen codeD)将用于与您指定作为您的数据参数JSON数据(数据:{I:I,S:S,B:B }
)将被正确地映射到您的操作参数....所以,除非你真的要发送JSON数据只是删除的contentType,你会没事的......
The Route is:
routes.MapRoute(
"Ajax", // Route name
"BizTalk/Services/{action}", // URL with parameters
new
{ // Parameter defaults
controller = "BizTalk"
}
);
My Controller is:
public JsonResult AjaxTest(string s, int i, bool b)
{
return Json("S: " + s + "," + "I: " + i + "," + "B: " + b);
}
My jQuery Code:
$(document).ready(function() {
$("#btn_test").click(function() {
var s = "test";
var i = 8;
var b = true;
$.ajax({
type: "POST", cache: false,
url: "/BizTalk/Services/AjaxTest",
data: { i: i, s: s, b: b },
contentType: "application/json; charset=utf-8",
dataType: "json",
success: function(msg) {
}
});
});
});
This post explains the problem and a possible solution (similar to how @Erv has explained).
If you remove contentType: "application/json; charset=utf-8"
from your call to jQuery.ajax
the default content type (form-urlencoded) will be used and the json data you have specified as your data parameter (data: { i: i, s: s, b: b }
) will be mapped correctly to your action parameters....so unless you really want to send json data just remove the contentType and you will be fine.....
这篇关于Ajax调用使用jQuery在ASP.NET MVC不传递参数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!