Maison  >  Questions et réponses  >  le corps du texte

javascript - js获取畅言的评论跨域问题怎么解决

$.getJSON('http://changyan.sohu.com/api/...',function(res){

    console.log(res);
});
跨域问题
黄舟黄舟2727 Il y a quelques jours357

répondre à tous(2)je répondrai

  • 黄舟

    黄舟2017-04-11 11:59:34

    纯前端处理,也就是使用jsonp方式,需要同另一端配合约定才行。很显然你不满足这种情况。
    所以,推荐使用服务端代理的方式,可以通过nginx代理,或者其他服务器代理。
    当然,也可以通过服务端代码的方式代理,也即通过服务端一个中转访问而已。

    répondre
    0
  • 黄舟

    黄舟2017-04-11 11:59:34

    前端不允许跨域。
    所以你还是通过服务器抓页面吧。

    当然如果你能控制被跨域的网站,可以使用jsonp
    不过我个人推荐控制header
    设置
    Access-Control-Allow-Origin
    来允许跨域

    répondre
    0
  • Annulerrépondre