Home  >  Q&A  >  body text

javascript - 前后端分离后你们是如何解决跨域问题的

联调接口 后台完全不管 目前准备用jsonp来做跨域 想问各位有没有什么更好的解决方法

伊谢尔伦伊谢尔伦2742 days ago1173

reply all(12)I'll reply

  • 天蓬老师

    天蓬老师2017-04-18 10:57:24

    Set response.setHeader("Access-Control-Allow-Origin", "*"); in the background
    You can also configure it on the nginx side to solve the problem.
    jsonp is not a particularly good method, it has size limitations on the data transferred.

    reply
    0
  • ringa_lee

    ringa_lee2017-04-18 10:57:24

    CORS。。。。

    reply
    0
  • PHP中文网

    PHP中文网2017-04-18 10:57:24

    CORS. Since it is front-end and back-end analysis, just set the header in the background

    reply
    0
  • PHP中文网

    PHP中文网2017-04-18 10:57:24

    This time I will start a node server to forward the request

    reply
    0
  • PHP中文网

    PHP中文网2017-04-18 10:57:24

    It is not recommended to use jsonp cross-domain. Although this ancient method has good compatibility, it has great limitations and has the risk of XSS.
    Relatively complete separation of front-end and back-end, of course, means that front-end personnel manage the web server. Of course, this requires relatively high requirements for front-end personnel.

    If you just want to adjust the interface, it is better to set up a local test server yourself. It is used as a static server for the page, which is convenient for adjusting the page, and can forward the request and request the data.
    You can use express yourself, or you can use ready-made ones like browsersync+gulp or webpack+hot reload server.

    I use browsersync, the configuration is very convenient, you can refer to this.

    reply
    0
  • 黄舟

    黄舟2017-04-18 10:57:24

    There is a chrome plug-in, what it does is response.setHeader("Access-Control-Allow-Origin", "*") , just turn it on.

    reply
    0
  • 阿神

    阿神2017-04-18 10:57:24

    Add nginx

    reply
    0
  • 阿神

    阿神2017-04-18 10:57:24

    WebSocket is not subject to cross-domain restrictions and can transmit any data, including jsonp.

    reply
    0
  • 天蓬老师

    天蓬老师2017-04-18 10:57:24

    By the way, shouldn’t this be decided before development? Isn’t it a little late to decide whether it’s jsonp or cors until joint debugging?

    reply
    0
  • PHPz

    PHPz2017-04-18 10:57:24

    No need for jsonp, it’s too inconvenient

    reply
    0
  • Cancelreply