路线是:
routes.MapRoute(
"Ajax", // Route name
"BizTalk/Services/{action}", // URL with parameters
new
{ // Parameter defaults
controller = "BizTalk"
}
);
我的 Controller 是:
public JsonResult AjaxTest(string s, int i, bool b)
{
return Json("S: " + s + "," + "I: " + i + "," + "B: " + b);
}
我的 jQuery 代码:
$(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) {
}
});
});
});
最佳答案
这个post解释了问题和可能的解决方案(类似于@Erv 的解释)。
如果您从对 jQuery.ajax
的调用中删除 contentType: "application/json; charset=utf-8"
,则默认内容类型 (form-urlencoded) 将被使用并且您指定为数据参数的 json 数据 (data: { i: i, s: s, b: b }
) 将正确映射到您的操作参数......所以除非你真的想发送 json 数据,只需删除 contentType 就可以了......
关于jquery - ASP.NET MVC 中使用 jQuery 进行 Ajax 调用不传递参数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/2785648/