【发布时间】:2015-02-14 12:10:21
【问题描述】:
我正在尝试将两个参数发布到以下函数,但我无法访问该函数:
public void SetShopSubCategories([FromBody]string userId, int []subCategories )
{
}
这就是我发帖的方式:
var subCategories = [ 1, 2, 3, 4, 5];
var userId = "123";
$.ajax({
type: "POST",
url: "/Category/SetShopSubCategories/",
contentType: 'application/json; charset=utf-8',
data: JSON.stringify(userId, subCategories),
success: function () {
alert("OK");
},
error: function () {
alert("error");
}
当我只使用一个参数发布时,它运行良好,我可以达到功能:
public void SetShopSubCategories([FromBody]string userId )
{
}
var userId = "123";
$.ajax({
type: "POST",
url: "/Category/SetShopSubCategories/",
contentType: 'application/json; charset=utf-8',
data: JSON.stringify(userId, subCategories),
success: function () {
alert("OK");
},
error: function () {
alert("error");
}
这个也不错:
public void SetShopSubCategories( int []subCategories )
{
}
var subCategories = [ 1, 2, 3, 4, 5];
$.ajax({
type: "POST",
url: "/Category/SetShopSubCategories/",
contentType: 'application/json; charset=utf-8',
data: JSON.stringify(subCategories),
success: function () {
alert("OK");
},
error: function () {
alert("error");
}
我的 RoutConfig:
routes.IgnoreRoute("{resource}.axd/{*pathInfo}");
GlobalConfiguration.Configuration.Routes.MapHttpRoute(
name: "SetCategories",
routeTemplate: "{controller}/{action}"
);
routes.MapRoute(
name: "Default",
url: "{controller}/{action}/{id}",
defaults: new { controller = "Home", action = "Index", id = UrlParameter.Optional }
);
【问题讨论】:
标签: c# jquery ajax asp.net-web-api