search

Home  >  Q&A  >  body text

javascript - How to solve cross-domain problems for the middle layer in node

I have tried to use nginx, but I don’t know nginx at all. After configuring it for a long time, I still can’t figure it out. Is there any other simple method that can easily solve the cross-domain problem?

大家讲道理大家讲道理2763 days ago835

reply all(2)I'll reply

  • 怪我咯

    怪我咯2017-06-28 09:25:00

    I don’t understand the role of your middle layer. Anyway, to solve the cross-domain problem, just add a field to the response header:
    Access-Control-Allow-Origin: *
    As for how to add it, you can use res.writeHead You can add it this way, or any other way. As long as you can see this header field in the final response returned to the browserok

    reply
    0
  • 某草草

    某草草2017-06-28 09:25:00

    //Configure node cross-domain

    app.all('*', (req, res, next) => {
      res.header('Access-Control-Allow-Origin', '*');
      res.header('Access-Control-Allow-Headers', 'X-Requested-With');
      res.header('Access-Control-Allow-Methods', "PUT, POST, GET, DELETE, OPTIONS");
      res.header('Content-Type', 'application/json;charset=utf-8');
      next();
    }) 

    reply
    0
  • Cancelreply