【问题标题】:YUI3 JSON parse and IE9YUI3 JSON 解析和 IE9
【发布时间】:2012-02-21 10:28:52
【问题描述】:

我正在尝试在 localhost 上使用 YUI3 测试一些 ajax 和 JSON 解析示例。代码如下:

<!doctype html>
<html>
<head>
    <title>YUI 3 Getting started</title>
</head>
<body>
    <p id="time">(what time is it?)</p>

    <button id="go">What time is it?</button>

    <script src="http://yui.yahooapis.com/3.3.0/build/simpleyui/simpleyui-min.js"></script>
    <script>

    function printTime(id, response) {
        try {
            var data = Y.JSON.parse(response.responseText);
        }
        catch (ex) {
            data = { time: "ERROR" };
        }

        Y.one("#time").setContent(data.time);
    }

    Y.one("#go").on("click", function () {
        Y.io("watch.php", {
            on: {
                success: printTime
            }
        });
    });

    </script>

</body>
</html>

这是watch.php:

<?php
header('Content-Type: application/json; charset=utf8');
echo(
    json_encode(
        array(
            "time" => date("g:ia l, M jS")
        )
    )
);

我已尝试将文件上传到网络主机,但仍然遇到同样的问题。奇怪的是,它在firefox中运行得很好,但在IE9和其他IE模式下却无法运行! IE 正在返回此错误代码:c00ce56e

有什么想法吗?

【问题讨论】:

    标签: internet-explorer-9 cross-browser


    【解决方案1】:

    事实证明 IE9 对 JSON 响应非常严格。可以肯定的是,请确保为每个 JSON 响应发送带有编码的正确标头。还要确保 JSON 格式正确。

    这是解决方法:

    header('Content-Type: application/json; charset=utf-8');
    

    请注意,字符集必须被定义并且必须是有效的字符集。

    【讨论】:

    • 确认 - 刚刚遇到了完全相同的大脑爆炸问题。请注意 utf-8 中的“-”——这就是这里的全部区别。
    猜你喜欢
    • 2013-02-10
    • 2011-11-03
    • 1970-01-01
    • 2016-11-04
    • 2017-05-31
    • 2016-11-08
    • 2018-03-11
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多