激情久久久_欧美视频区_成人av免费_不卡视频一二三区_欧美精品在欧美一区二区少妇_欧美一区二区三区的

服務器之家:專注于服務器技術及軟件下載分享
分類導航

云服務器|WEB服務器|FTP服務器|郵件服務器|虛擬主機|服務器安全|DNS服務器|服務器知識|Nginx|IIS|Tomcat|

服務器之家 - 服務器技術 - Nginx - nginx服務器通過配置來解決API的跨域問題

nginx服務器通過配置來解決API的跨域問題

2019-11-28 14:38YoYong Nginx

這篇文章主要給大家介紹了關于nginx服務器是如何配置來解決API跨域問題的相關資料,文中介紹的非常詳細,對大家具有一定的參考學習價值,需要的朋友們下面來一起看看吧。

前言

最近在采用jquery ajax調用http請求時,發現了一系列問題:

如采用firebug調試API請求(這個API是自己服務器的應用),看到服務器明明返回200狀態,response返回數據也是json格式,但ajax返回的error。

在排除json數據格式不正確的原因之后,發現了ajax error函數返回“networkerror failed to execute ‘send' on ‘xmlhttprequest' failed to load ‘http //“ XMLHttpRequest.status=0,就是沒有初始化。

后來才知道是跨域問題(CORS),因為程序調用的是遠程服務器的API,服務器不允許跨域調用。如果只是簡單的方法,只需要在程序的response添加支持跨域的header添加屬性”Access-Control-Allow-Origin: * “即可。

如java 服務器代碼:

?
1
2
3
4
yourownvariable.setHeader("Access-Control-Allow-Origin:", "origin url of your site");
 
 
yourownvariable.setHeader("Access-Control-Allow-Methods", "GET, POST,PUT");

如果是配置nginx服務器(如果是其他服務器,可以參考:I want to add CORS support to my server),需要在nginx.conf配置文件添加一下內容:

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
#
# 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';
}
}

總結

以上就是這篇文章的全部內容了,希望本文的內容對大家的學習或者工作能帶來一定的幫助,如果有疑問大家可以留言交流,謝謝大家對服務器之家的支持。

原文鏈接:http://www.yoyong.com/archives/985

延伸 · 閱讀

精彩推薦
主站蜘蛛池模板: 亚洲乱搞 | 国产伦久视频免费观看视频 | 色婷婷a| 日本高清无遮挡 | 日韩中字幕 | 国产精品视频海角社区88 | 精品国产一区二区三区久久久蜜月 | 黄色av网站在线观看 | 黄色免费视频观看 | 亚洲国产超高清a毛毛片 | 欧美视频一区二区三区在线观看 | 国产xxxxx在线观看 | chinese军人gay呻吟 | 亚洲导航深夜福利涩涩屋 | 欧美精品国产综合久久 | 亚洲国产精品久久久久久久久久久 | 亚洲第一页综合 | 国产永久免费观看 | 中文字幕亚洲一区二区三区 | 一区二区三区手机在线观看 | av电影免费播放 | 欧美日韩爱爱视频 | 国产福利视频在线观看 | 日韩一级片免费 | 91短视频在线观看免费最新 | 一级国产航空美女毛片内谢 | 亚洲精品欧美二区三区中文字幕 | 国产一区日韩一区 | 精品中文字幕视频 | 91午夜理伦私人影院 | 国产一级aa大片毛片 | 久久免费视频3 | 一级黄色毛片播放 | 91精彩在线| 91一级毛片 | 久草成人在线 | 一级做a爱片性色毛片 | 久久国产精品二国产精品 | 一级影片在线观看 | 天天夜夜操操 | 国产永久免费观看 |