如何使用jquery ajax jsonp将跨域json数据显示为html?

时间:2017-02-14 03:06:00

标签: javascript jquery ajax cross-domain jsonp

您好我有两个不同的域名,我在domain1.com中提供了json文件,在domain2.com中提供了我的ajax调用(html)文件。现在我正在尝试将以下json数据打印到我的html页面中。

    [{
    "userID": 506,
    "userName": "Alan",
    "userAge": "alan@somewhere.com",
    "userGender": "male",
    "userNo": 5,
    "userStreet": "abx,street",
    "userCity": "chennai",
    "userCountry": "singapore",
    "userPostal": "545445",
    "userProduct": "samsung",
    "userQty": "4",
    "userPrice": "600"
}, {
    "userID": 566,
    "userName": "bob",
    "userAge": "bob@somewhere.com",
    "userGender": "female",
    "userNo": 8,
    "userStreet": "box,street",
    "userCity": "hydrbad",
    "userCountry": "USA",
    "userPostal": "8787",
    "userProduct": "PIXEL",
    "userQty": "7",
    "userPrice": "3400"
}]

我正在尝试使用以下代码将上述json数据显示到我的html页面中,但我没有得到任何结果。有人可以帮我吗?

<html>
<head>
    <script type="text/javascript" src="https://ajax.googleapis.com/ajax/libs/jquery/1.6.2/jquery.min.js"></script>
    <script>
        $(document).ready(function(){
        //alert("hi");
            $.ajax({
                url: 'http://stemgy.com/d.json',
                dataType: 'jsonp',
                success: function(dataWeGotViaJsonp){
                alert(dataWeGotViaJsonp);
                    var text = '';
                    var len = dataWeGotViaJsonp.length;
                    alert(len);

                }
            });
        })
    </script>
</head>
<body>
    <div id='response'></div>
</body>

0 个答案:

没有答案