Ajax 如何使控制器从post正文读取对象(请求的资源不支持http方法';post)
我尝试使用post ajax从控制器获取数据,而不更改url(通过在post正文中插入值) 使用下面的代码,但服务器似乎会以 “请求的资源不支持http方法Ajax 如何使控制器从post正文读取对象(请求的资源不支持http方法';post),ajax,json,asp.net-mvc-4,post,asp.net-web-api,Ajax,Json,Asp.net Mvc 4,Post,Asp.net Web Api,我尝试使用post ajax从控制器获取数据,而不更改url(通过在post正文中插入值) 使用下面的代码,但服务器似乎会以 “请求的资源不支持http方法'POST” 如何使控制器从post对象读取主体 控制器类*** public AllAppType Post(int limit) { using (var ms = new MySampleServiceClient(Wcf.Routing.RouterBindings.Local,
'POST
”
如何使控制器从post对象读取主体
控制器类***
public AllAppType Post(int limit)
{
using (var ms = new MySampleServiceClient(Wcf.Routing.RouterBindings.Local,
Wcf.Routing.RouterAddresses.Local.RequestReply))
{
if (limit == 1)
{
var objectToSerialize = new AllAppType();
objectToSerialize.Band = new List<Banditem>
{
new Banditem { Id= 1, Name = "Potato", Detail = "ปากดี" },
new Banditem { Id= 2, Name = "Body Slam", Detail = "ขอบฟ้า" },
new Banditem { Id= 3, Name = "Kala", Detail = "ไปเป็นแฟนกันตั้งแต่เมื่อไ
new Banditem { Id= 4, Name = "Bigass", Detail = "ดีแต่ปาก"},
new Banditem { Id= 5, Name = "Paradox", Detail = "ฤดูร้อน"},
};
return objectToSerialize;
}
else if (limit == 2)
{
var objectToSerialize = new AllAppType();
objectToSerialize.Band = new List<Banditem>
{
new Banditem { Id= 6, Name = "Test1", Detail = "111" },
new Banditem { Id= 7, Name = "Test2", Detail = "222" },
new Banditem { Id= 8, Name = "Test3", Detail = "333"},
new Banditem { Id= 9, Name = "Test4", Detail = "444"},
new Banditem { Id= 10, Name = "Test5", Detail = "555"},
};
return objectToSerialize;
}
else
{
var objectToSerialize = new AllAppType();
objectToSerialize.Band = new List<Banditem>
{
new Banditem { Id= 11, Name = "Cnblue", Detail = "CCC" },
new Banditem { Id= 12, Name = "Ft island", Detail = "FFF" },
new Banditem { Id= 13, Name = "Girl generation", Detail = "GGG"},
new Banditem { Id= 14, Name = "Miss-A", Detail = "AAA"},
new Banditem { Id= 15, Name = "SNSD", Detail = "SSS"}
};
return objectToSerialize;
}
}
}
public AllAppType Post(整数限制)
{
使用(var ms=new MySampleServiceClient(Wcf.Routing.RouterBindings.Local,
Wcf.Routing.RouterAddresses.Local.RequestReply)
{
如果(限制==1)
{
var objectToSerialize=new AllAppType();
objectToSerialize.Band=新列表
{
新名称{Id=1,Name=“Potato”,Detail=”ปากดี" },
new Banditem{Id=2,Name=“Body Slam”,Detail=”ขอบฟ้า" },
新的{Id=3,Name=“Kala”,Detail=”ไปเป็นแฟนกันตั้งแต่เมื่อไ
new Banditem{Id=4,Name=“Bigass”,Detail=”ดีแต่ปาก"},
新的{Id=5,Name=“Paradox”,Detail=”ฤดูร้อน"},
};
返回对象序列化;
}
否则如果(限制==2)
{
var objectToSerialize=new AllAppType();
objectToSerialize.Band=新列表
{
new Banditem{Id=6,Name=“Test1”,Detail=“111”},
new Banditem{Id=7,Name=“Test2”,Detail=“222”},
新的{Id=8,Name=“Test3”,Detail=“333”},
new Banditem{Id=9,Name=“Test4”,Detail=“444”},
new Banditem{Id=10,Name=“Test5”,Detail=“555”},
};
返回对象序列化;
}
其他的
{
var objectToSerialize=new AllAppType();
objectToSerialize.Band=新列表
{
new Banditem{Id=11,Name=“Cnblue”,Detail=“CCC”},
new Banditem{Id=12,Name=“Ft岛”,Detail=“FFF”},
新班迪姆{Id=13,Name=“女孩一代”,Detail=“GGG”},
新的{Id=14,Name=“Miss-A”,Detail=“AAA”},
new Banditem{Id=15,Name=“SNSD”,Detail=“SSS”}
};
返回对象序列化;
}
}
}
html***
function runpost1() {
$('#postresult').text("");
answer.clearAttributes;
$.ajax({
url: "All",
type: "post",
content: "application/json; charset=utf-8",
data: {limit :1 },
dataType: "json",
success: function (answer) {
$.each(answer.Band, function (key, item) {
// Add a list item for the product.
$('<li>', { text: formatItem(item) }).appendTo($('#postresult'));
});
// $("#answer").text(JSON.stringify(answer));
}
});
}
函数runpost1(){
$('#postresult')。文本(“”);
答案:清晰的属性;
$.ajax({
网址:“全部”,
类型:“post”,
内容:“application/json;charset=utf-8”,
数据:{限制:1},
数据类型:“json”,
成功:功能(答案){
$。每个(答案、波段、功能(键、项目){
//为产品添加列表项。
$(“”,{text:formatItem(item)}).appendTo($(“#postresult”);
});
//$(“#答案”).text(JSON.stringify(答案));
}
});
}
您需要一个方法发布到公共ActionResult MyMethod(int-limit){//build your collection return Json(objectToSerialize);}
中,然后在ajax函数中使用url:'@url.Action(“MyMethod”,“yourControllerName”),
-调用它,并删除内容:“application/Json;charset=utf-8”“,
@StephenMuecke谢谢你,先生,但是它说不能隐式地将type System.Web.Http.Results.JsonResult转换为SampleApp.Allcontrolelr.Actionresult我该如何修复它我假设你的控制器中一定有一些不正确的using语句-你需要System.Web.Mvc
(或者这是ApiController
)@StephenMuecke是的,当您需要属性标记您的问题时,它是Apicontroller!将方法更改为public System.Web.Http.Results.JsonResult MyMethod(int limit)
您需要一个方法来发布到public ActionResult MyMethod(int limit){//build your collection return Json(objectToSerialize);}
然后在ajax函数中使用url:'@url.Action(“MyMethod”,“yourControllerName”),
调用它,并删除内容:“application/json;charset=utf-8”,
@StephenMuecke谢谢你,先生,但是它说不能隐式地将type System.Web.Http.Results.JsonResult转换为SampleApp.Allcontrolelr.Actionresult我该如何修复它我假设你的控制器中一定有一些不正确的using语句-你需要System.Web.Mvc
(或者这是ApiController
)@StephenMuecke是的,当您需要属性标记您的问题时,它是Apicontroller!将方法更改为public System.Web.Http.Results.JsonResult MyMethod(int limit)
您需要一个方法来发布到public ActionResult MyMethod(int limit){//build your collection return Json(objectToSerialize);}
然后在ajax函数中使用url:'@url.Action(“MyMethod”,“yourControllerName”),
调用它,并删除内容:“application/json;charset=utf-8”,
@StephenMuecke谢谢你,先生,但是它说不能隐式地将type System.Web.Http.Results.JsonResult转换为SampleApp.Allcontrolelr.Actionresult我该如何修复它我假设你的控制器中一定有一些不正确的using语句-你需要System.Web.Mvc
(或者这是ApiController
)@StephenMuecke是的,当您需要属性标记问题时,它是Apicontroller!将方法更改为public System.Web.Http.Results.jsonresult