日期:2014-05-17  浏览次数:20477 次

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的时候 才用到。