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

有关jsAjax和jqAjax的问题,先谢谢了!
获取json数据是在以下的连接:
http://services.digg.com/stories/top?appkey=http%3A%2F%2Fmashup.com&type=javascript&callback=?
这没问题!

问题是用jsAjax访问是拒绝访问,
而用jqAjax就可以访问的,
1.这是什么道理?
2.怎样用jsAjax访问?

以下是代码:
jsAjax代码:提示拒绝访问
JScript code
        <script type ="text/javascript">
            onload = function() {
                var xhr = new XMLHttpRequest();
                var url = "http://services.digg.com/stories/top?appkey=http%3A%2F%2Fmashup.com&type=javascript&callback=?";
                xhr.open("get", url, true);

                xhr.onreadystatechange = function() {
                    if (xhr.readyState == 4) {
                        if (xhr.status == 200) {
                            var jsonData = eval("(" + xhr.responseText + ")"); //得到JSON对象
                            alert(jsonData.stories.length); //得到json数据数量
                            //......
                        }
                    }
                }
                xhr.send();
            }
        </script>

jqAjax代码:访问没问题,得到数值是15
JScript code
    <script type="text/javascript">
        $().ready(function() {
            var url = "http://services.digg.com/stories/top?appkey=http%3A%2F%2Fmashup.com&type=javascript&callback=?";
            $.getJSON(url, function(data) {
                alert(data.stories.length);
            });
        });
    </script> 


------解决方案--------------------
http://blog.csdn.net/sunqunsunqun/article/details/7428069
------解决方案--------------------
HTML code

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title>融化这个BUG。</title>
    <script type="text/javascript">
        var list = null;
        function callbackFun(o) {
            list = o;
        }
    </script>
    <script src="http://services.digg.com/stories/top?appkey=http%3A%2F%2Fmashup.com&type=javascript&callback=callbackFun" type="text/javascript"></script>
</head>
<body>
    <script type="text/javascript">
        document.write(list.count);
    </script>
</body>
</html>