[转载]若要允许 GET 请求,请将 JsonRequestBehavior 设置为 AllowGet – 露水丛生 – 博客园
- ASP.NET MVC
- 2015-01-22
- 147热度
- 0评论
[转载]若要允许 GET 请求,请将 JsonRequestBehavior 设置为 AllowGet - 露水丛生 - 博客园.
请将 JsonRequestBehavior 设置为 AllowGet
MVC 默认 Request 方式为 Post。
action
[csharp]
public JsonResult GetPersonInfo() {
var person = new {
Name = "张三",
Age = 22,
Sex = "男"
};
return Json(person);
}
[/csharp]
或者
[csharp]
public JsonResult GetPersonInfo() {
return Json (new{Name = "张三",Age = 22,Sex = "男"});
}
view
$.ajax({
url: "/FriendLink/GetPersonInfo",
type: "POST",
dataType: "json",
data: { },
success: function(data) {
$("#friendContent").html(data.Name);
}
})
[/csharp]
POST 请求没问题,GET 方式请求出错:

解决方法
json方法有一个重构:
[csharp]
public JsonResult GetPersonInfo() {
var person = new {
Name = "张三",
Age = 22,
Sex = "男"
};
return Json(person,JsonRequestBehavior.AllowGet);
}
[/csharp]
这样一来我们在前端就可以使用Get方式请求了:
[csharp]
$.getJSON("/FriendLink/GetPersonInfo", null, function(data) {
$("#friendContent").html(data.Name);
})
[/csharp]