search

Home  >  Q&A  >  body text

node.js+express如何跨域访问数据

http://pv.sohu.com/cityjson?ie=utf-8

我想在以下代码中的cip,cname...里面获取到上面网页中的数据,可以在服务器端直接跨域获取到吗?CORS可以做到吗,可以的话具体怎么做?

router.post('/giveLike', function(req, res) {
    var admin = {
        cip: 
        cname:
        cid:
    }
    Admin.checkLiked(ip, function(err, status) {
        if(err) {
            res.send('Database Connect Error, Please try again later');
        }
        if (!status) {
            Admin.saveLike(ip, function(err, admin) {
                if (err) {
                    res.send('Database Connect Error, Please try again later');
                }
                count = count + 1;
                req.session.likesCount = count;
                res.send(req.session.likesCount);
            });
        }
        else {
            res.send('existed');
        }
    });
}); 
大家讲道理大家讲道理2778 days ago574

reply all(2)I'll reply

  • 怪我咯

    怪我咯2017-04-17 13:54:36

    As mentioned above.

    Please first confirm where the code you use to obtain sohu data is running

    If it is in the browser, then you need to consider cross-domain issues

    If it is on the server side, then you do not need to consider cross-domain issues, you only need to simulate the request to obtain data on the server side

    In addition, let’s talk about CORS. If I remember correctly, CORS is set on the target machine, that is, it is set on the sohu service.
    This mark is either set, even if it is not set, I think you may not be able to modify sohu’s services (internal matters will be discussed separately)

    reply
    0
  • PHP中文网

    PHP中文网2017-04-17 13:54:36

    I don’t understand what you mean. The same-origin policy is a policy used to restrict client js. There is no cross-domain restriction on the server

    reply
    0
  • Cancelreply