读书人

MVC(Json) 出现提示上载

发布时间: 2012-11-10 10:48:51 作者: rapoo

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

读书人网 >asp.net

热点推荐