请将 JsonRequestBehavior 设置为 AllowGet

MVC 默认 Request 方式为 Post。
action

复制代码
public JsonResult GetPersonInfo()  {  
  var person = new  {  
    Name = "张三",  
    Age = 22,  
    Sex = ""  
  };  
  return Json(person);  
}  
复制代码

或者

复制代码
 1 public JsonResult GetPersonInfo()  {  
 2   return Json (new{Name = "张三",Age = 22,Sex = "男"});  
 3 }  
 4 view  
 5 $.ajax({  
 6   url: "/FriendLink/GetPersonInfo",  
 7   type: "POST",  
 8   dataType: "json",  
 9   data: { },  
10   success: function(data) {  
11      $("#friendContent").html(data.Name);  
12   }  
13 })  
复制代码

POST 请求没问题,GET 方式请求出错:

 

解决方法
json方法有一个重构:

复制代码
1 public JsonResult GetPersonInfo()  {  
2   var person = new  {  
3       Name = "张三",  
4       Age = 22,  
5       Sex = ""  
6    };  
7    return Json(person,JsonRequestBehavior.AllowGet);  
8 }  
复制代码

这样一来我们在前端就可以使用Get方式请求了:

1 $.getJSON("/FriendLink/GetPersonInfo", null, function(data) {  
2     $("#friendContent").html(data.Name);  
3 })  

 范例来自 http://www.cnblogs.com/Steven7Gao/archive/2012/06/13/2547905.html

posted on 2012-07-31 16:16  露水丛生  阅读(3780)  评论(0编辑  收藏  举报