首先说结论:设置返回String类型的, Response.ContentType = "application/json";等于使用JsonResult。
证据 1:设置返回String类型的, Response.ContentType = "application/json"。JQ会自动把JSON字符串转换为JSON对象。
如果不设置 Response.ContentType = "application/json",则是使用默认值Response.ContentType = "text/html"。这样JQ就不会把JSON字符串转换为JSON对象。
看如下两个实例:
---------------------------------------------------------------
![](https://images0.cnblogs.com/blog2015/500844/201503/180953007511002.jpg)
-----------------
![](https://images0.cnblogs.com/blog2015/500844/201503/180953312985267.jpg)
-----------------
![](https://images0.cnblogs.com/blog2015/500844/201503/180954012679188.jpg)
------------------------------------------------------------------------
------------------------------------------------------------------------
![](https://images0.cnblogs.com/blog2015/500844/201503/180954155956399.jpg)
![](https://images0.cnblogs.com/blog2015/500844/201503/180954450486464.jpg)
![](https://images0.cnblogs.com/blog2015/500844/201503/180955046104607.jpg)
证据 2:设置JsonResult 的ContentType = "text/html",这样JQ就不会把JSON字符串转换为JSON对象。
反之,使用默认值就是ContentType = "application/json ",JQ会自动把JSON字符串转换为JSON对象。
看如下两个实例:
----------------------------------------------------------------------
![](https://images0.cnblogs.com/blog2015/500844/201503/180955261738794.jpg)
---------------------------
![](https://images0.cnblogs.com/blog2015/500844/201503/180955399235662.jpg)
--------------------------
![](https://images0.cnblogs.com/blog2015/500844/201503/180956228132565.jpg)
----------------------------------------------------------------------------
----------------------------------------------------------------------------
![](https://images0.cnblogs.com/blog2015/500844/201503/180956364709404.jpg)
-----------------------------------------------------------
![](https://images0.cnblogs.com/blog2015/500844/201503/180956588603777.jpg)
-------------------------------------------------------------
![](https://images0.cnblogs.com/blog2015/500844/201503/180957102675729.jpg)
----------------------------------------------------------------------------
如上证据证明了,我的结论。
如下在记几点:
1.当返回到前端的json数据,不标准时如--"{"Age":"fdasfdasf","name":"rer"}",前端抓包的值和我给的一样。不管你是用
JsonResult和返回String类型,都需要手动, JSON.parse(Data),才能把字符串变为JSON对象。
1.当返回到前端的json数据,标准时如--"{"Age":"fdasfdasf","name":"rer"}",前端抓包的值和我给的一样。
只需要保证响应报文头的ContentType = "application/json,JQ 都会自动把JSON字符串转换为JSON对象。
2014.09.02 六欲 在广州