search

Home  >  Q&A  >  body text

vue.js - After vue is packaged, the cross-domain problem of nginx server API request cannot be solved.

I packaged vue and sent it to the server, then placed it in the directory specified by the www subdomain name. When I make a request, I need to request the directory bound to the api subdomain name. This request caused a cross-domain prompt

Then I also configured it in nginx but there was no solution.

That is, I request www.daimatu.cn/api/login/index ajax post request and forward it to api.daimatu.cn/api/login/index

I have made a lot of settings in location / {} and location /api {} before. They were all obtained from Baidu online, but they didn’t work~!

Then I changed the requested address to online in my local computer, and there was no problem with local requests! It is passable and can operate normally. I compared the request hair on both sides and found that they are different! The Request Method of the local request to the online server is a normal GET POST. However, when I go from www to request the api online, the Request Method becomes OPTIONS instead of the normal GET POST and other request headers. I also searched and found how to set the OPTIONS request. At this time, cross-domain still cannot be solved!

As shown in the picture, it has become options

I would like to ask everyone how you configure it. My back-end language is PHP. Don't ask me to set up and change PHP. My local requests for local services and online services are all normal. There is only a problem with the online www forwarding request api. So no need to change the program.

Api request address: http://api.daimatu.cn/api/log...
Api account: admin
Api password: 123456
Api verification code: 1234
Please everyone The NGINX server asks me to use this API to check your configuration. I have been struggling for a long time without success! !

世界只因有你世界只因有你2716 days ago1750

reply all(1)I'll reply

  • 阿神

    阿神2017-06-26 10:51:58

    Make the following configuration in location

    add_header 'Access-Control-Allow-Origin' '*';
    add_header 'Access-Control-Allow-Credentials' 'true';
    add_header 'Access-Control-Allow-Methods' 'GET, POST, OPTIONS';

    Then restart the nginx service

    service nginx reload

    reply
    0
  • Cancelreply