程序代码
代码如下 | 复制代码 |
<script type="text/javascript">
$(document).ready(function () { var myChart = new FusionCharts("/FusionCharts/FCF_MSLine.swf", "myChart1", "720", "300"); myChart.setDataURL("/Portal/GetUserStat"); myChart.render("charts1"); }) </script> |

直觉和字符编码有关。在浏览器中将MVC接口数据另存为xml文件,FusionCharts直接调用xml文件同样是乱码,再用UltraEdit打开xml另存为UTF8带BOM文件,图表显示正常,前后两个xml文件:

为了确保BOM不是在浏览器中另存为时丢失,对http数据抓包,确定了服务器响应时就没有BOM:

现在可以肯定是因为MVC输出的内容缺少BOM导致乱码,怎么改呢?找到代码:
代码如下 | 复制代码 |
return Content(str, "text/xml", Encoding.UTF8);
|
代码如下 | 复制代码 |
return Content(str, "text/xml", new UTF8Encoding(true));
|
仍是缺少BOM乱码,最后修改如下解决:
代码如下 | 复制代码 |
Response.Write(Encoding.UTF8.GetString(new byte[] { 0xEF, 0xBB, 0xBF }));
return Content(str, "text/xml", Encoding.UTF8); |