首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > 网站开发 > asp.net >

MVC(Json) 出现提示上载

2012-11-10 
MVC(Json) 出现提示下载public JsonResult Index(){JsonResult json new JsonResult{Data new{Name

MVC(Json) 出现提示下载
public JsonResult Index()
  {
  JsonResult json = new JsonResult
  {
  Data = new
  {
  Name = "zzl",
  Sex = "male" 
  }
  };
   
  return Json(json,"text/html",JsonRequestBehavior.AllowGet);
  }
  $(document).ready(function () {
  var url = '@Url.Action("Index", "Home")';
  $.ajax
  ({ url: url,
  dataType: "json",
  cache: false,
  data: null,
  type: "POST",
  success: function (data)
  { alert(data.Data.Sex); }
   
  });
  });
为什么没有弹出正确值呢,控制器加上"text/html"才不会出现提示下载,求解

[解决办法]
[HttpGet]
public JsonResult Index()
{
...
return Json(json,JsonRequestBehavior.AllowGet);
}

$.ajax
({ url: url,
dataType: "json",
cache: false,
data: null,
type: "GET",
success: function (data)
{ alert(data.Data.Sex); }

});
});
[解决办法]
同意楼上,直接return json就行了,不需要设置ContentType
[解决办法]
ContentType 只有在 POST的时候 才用到。

热点排行