-
When doing a project, the design of front-end separation is adopted, and the rest-style HTTP interface is designed to meet the background service call and the front-end to use the AJAX call directly. , then encountered a cross-domain problem, the background is the spring MVC structure, the HTTP method supports GET, post, put, delete, option and other methods, because at the time of the post call, the front-end HTTP will first take option to the server side, 204 and then submit the request data, in addition to the nginx level to do the adjustment, Spring Web. XML also needs to be set up, this scheme does not need to add filter on the server and other things, the possible version is as follows:
-
Spring Web. XML
<servlet><servlet-name>root</servlet-name><servlet-class> Org.springframework.web.servlet.dispatcherservlet</servlet-class><init-param><param-name> contextconfiglocation</param-name><param-value>classpath:applicationcontext-web.xml</ Param-value></init-param><init-param><param-name>dispatchoptionsrequest</param-name ><param-value>true</param-value></init-param><load-on-startup>1</load-on-startup ></servlet><servlet-mapping><servlet-name>root</servlet-name><url-pattern>/ </url-pattern></servlet-mapping>
Location ~/api/* {dav_methods PUT DELETE; if ($request _method = ' OPTIONS ') {add_header ' access-control-allow-origin ' $http _origin '; Add_header ' Access-control-allow-methods ' GET, Post,put,delete, OPTIONS '; Add_header ' Access-control-allow-headers ' authorization,dnt,x-customheader,keep-alive, User-Agent,X-Reques Ted-with,if-modified-since,cache-control,content-type '; Add_header ' access-control-max-age ' 3600; Add_header ' Content-type ' Text/plain charset=utf-8 '; Add_header ' content-length ' 0; return 204; } if ($request _method = ' POST ') {add_header ' access-control-allow-origin ' $http _origin '; Add_header ' Access-control-allow-methods ' GET, Post,put,delete, OPTIONS '; Add_header ' Access-control-allow-headers ' authorization,dnt,x-customheader,keep-alive, User-Agent, X-requested-with,if-modified-since,cache-control,content-type '; } if ($request _method = ' GET ') {add_header ' access-control-allow-origin ' $http _origin '; Add_header ' Access-control-allow-methods ' GET, post,delete,put, OPTIONS '; Add_header ' Access-control-allow-headers ' authorization,dnt,x-customheader,keep-alive, User-Agent,X-Reques Ted-with,if-modified-since,cache-control,content-type '; } if ($request _method = ' PUT ') {add_header ' access-control-allow-origin ' $http _origin '; Add_header ' Access-control-allow-methods ' GET, Post,put,delete, OPTIONS '; Add_header ' Access-control-allow-headers ' authorization,dnt,x-customheader,keep-alive, User-Agent,X-Reques Ted-with,if-modified-since,cache-control,content-type '; } if ($request _method = ' DELETE ') {add_header ' Access-control-allow-origin ' $http _Origin '; Add_header ' Access-control-allow-methods ' GET, post,delete,put, OPTIONS '; Add_header ' Access-control-allow-headers ' authorization,dnt,x-customheader,keep-alive, User-Agent,X-Reques Ted-with,if-modified-since,cache-control,content-type '; } root/opt/www/web/; Index index.jsp; Proxy_pass <a rel= "nofollow" href= "http://localhost.html" target= "_blank" > <a rel= "nofollow" href= "htt P://localhost "target=" _blank ">http://localhost</a></a>:8089; include/opt/conf/nginx/proxy.conf; }
Above is the Nginx cross-domain setting nginx nginx php nginx rewrite content, more relevant content please pay attention to topic.alibabacloud.com (www.php.cn)!