nginx服務(wù)器通過配置來解決API的跨域問題
前言
最近在采用jquery ajax調(diào)用http請求時(shí),發(fā)現(xiàn)了一系列問題:
如采用firebug調(diào)試API請求(這個(gè)API是自己服務(wù)器的應(yīng)用),看到服務(wù)器明明返回200狀態(tài),response返回?cái)?shù)據(jù)也是json格式,但ajax返回的error。
在排除json數(shù)據(jù)格式不正確的原因之后,發(fā)現(xiàn)了ajax error函數(shù)返回“networkerror failed to execute ‘send' on ‘xmlhttprequest' failed to load ‘http //“ XMLHttpRequest.status=0,就是沒有初始化。
后來才知道是跨域問題(CORS),因?yàn)槌绦蛘{(diào)用的是遠(yuǎn)程服務(wù)器的API,服務(wù)器不允許跨域調(diào)用。如果只是簡單的方法,只需要在程序的response添加支持跨域的header添加屬性”Access-Control-Allow-Origin: *
“即可。
如java 服務(wù)器代碼:
yourownvariable.setHeader("Access-Control-Allow-Origin:", "origin url of your site"); yourownvariable.setHeader("Access-Control-Allow-Methods", "GET, POST,PUT");
如果是配置nginx服務(wù)器(如果是其他服務(wù)器,可以參考:I want to add CORS support to my server),需要在nginx.conf配置文件添加一下內(nèi)容:
# # Wide-open CORS config for nginx # location / { if ($request_method = 'OPTIONS') { add_header 'Access-Control-Allow-Origin' '*'; add_header 'Access-Control-Allow-Methods' 'GET, POST, OPTIONS'; # # Custom headers and headers various browsers *should* be OK with but aren't # add_header 'Access-Control-Allow-Headers' 'DNT,X-CustomHeader,Keep-Alive,User-Agent,X-Requested-With,If-Modified-Since,Cache-Control,Content-Type,Content-Range,Range'; # # Tell client that this pre-flight info is valid for 20 days # add_header 'Access-Control-Max-Age' 1728000; 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' '*'; add_header 'Access-Control-Allow-Methods' 'GET, POST, OPTIONS'; add_header 'Access-Control-Allow-Headers' 'DNT,X-CustomHeader,Keep-Alive,User-Agent,X-Requested-With,If-Modified-Since,Cache-Control,Content-Type,Content-Range,Range'; add_header 'Access-Control-Expose-Headers' 'DNT,X-CustomHeader,Keep-Alive,User-Agent,X-Requested-With,If-Modified-Since,Cache-Control,Content-Type,Content-Range,Range'; } if ($request_method = 'GET') { add_header 'Access-Control-Allow-Origin' '*'; add_header 'Access-Control-Allow-Methods' 'GET, POST, OPTIONS'; add_header 'Access-Control-Allow-Headers' 'DNT,X-CustomHeader,Keep-Alive,User-Agent,X-Requested-With,If-Modified-Since,Cache-Control,Content-Type,Content-Range,Range'; add_header 'Access-Control-Expose-Headers' 'DNT,X-CustomHeader,Keep-Alive,User-Agent,X-Requested-With,If-Modified-Since,Cache-Control,Content-Type,Content-Range,Range'; } }
總結(jié)
以上就是這篇文章的全部內(nèi)容了,希望本文的內(nèi)容對大家的學(xué)習(xí)或者工作能帶來一定的幫助,如果有疑問大家可以留言交流,謝謝大家對腳本之家的支持。
相關(guān)文章
Nginx+Tomcat反向代理與負(fù)載均衡的實(shí)現(xiàn)
這篇文章給大家詳細(xì)介紹了如何實(shí)現(xiàn)Nginx+Tomcat反向代理與負(fù)載均衡,文中的流程步驟介紹的非常詳細(xì)對我們的學(xué)習(xí)或工作有一定的幫助,需要的朋友可以參考下2023-07-07Nginx本地目錄映射實(shí)現(xiàn)代碼實(shí)例
這篇文章主要介紹了Nginx本地目錄映射實(shí)現(xiàn)代碼實(shí)例,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下2020-10-10重啟或殺掉Nginx進(jìn)程后丟失nginx.pid的解決辦法
在重啟或殺掉nginx進(jìn)程后,會丟失nginx.pid文件,導(dǎo)致nginx無法正常啟動,這里分享下解決方法2014-01-01Windows安裝nginx1.10.1反向代理訪問IIS網(wǎng)站
這篇文章主要為大家詳細(xì)介紹了Windows安裝nginx1.10.1反向代理訪問IIS網(wǎng)站的相關(guān)資料,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2016-11-11nginx找到默認(rèn)根目錄(root?html)的方法
這篇文章主要給大家介紹了nginx如何找到默認(rèn)根目錄(root?html),文中給出詳細(xì)的解決方法,通過代碼示例講解的非常詳細(xì),具有一定的參考價(jià)值,需要的朋友可以參考下2023-11-11