320 访问非同源数据 服务器端解决方案

3000端口的客户端的代码

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Document</title>
</head>
<body>
    <button id="btn">点我发送请求</button>
    <script src="/js/ajax.js"></script>
    <script>
        // 获取按钮
        var btn = document.getElementById('btn');
        // 为按钮添加点击事件
        btn.onclick = function () {
            ajax({
                type: 'get',
                url: 'http://localhost:3000/server',
                success: function (data) {
                    console.log(data);
                }
            })
        };
    </script>
</body>
</html>

3000端口的服务器端的代码

app.get('/server', (req, res) => {
    request('http://localhost:3001/cross', (err, response, body) => {
        res.send(body);
    })
});

3001端口的服务器端的代码

app.get('/cross', (req, res) => {
    res.send('ok')
});

猜你喜欢

转载自www.cnblogs.com/jianjie/p/12355752.html
今日推荐