AJAX跨域请求-用JSONP获取股票数据

@spiritree  January 20, 2017

教程来自http://www.liaoxuefeng.com/

1.准备好回调函数

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>获取股票价格</title>
</head>
<body>
<p id="netease-jsonp"></p>
<p><button type="button" onclick="getPrice()">获取价格</button></p>
<script>
    function refreshPrice(data) {
    let p = document.getElementById('netease-jsonp');
    p.innerHTML = data['0000001'].name + ':' +
            data['0000001'].price;
}
</script>
<script src="js/getstockprice.js"></script>
</body>
</html>

2.接受价格函数

function getPrice() {
    let
        js = document.createElement('script'),
        head = document.getElementsByTagName('head')[0];
    js.src = 'http://api.money.126.net/data/feed/0000001,1399001?callback=refreshPrice';
    head.appendChild(js);
}

效果图

![](
https://om4v1ul08.qnssl.com/getp.png)


添加新评论