一区二区三区在线-一区二区三区亚洲视频-一区二区三区亚洲-一区二区三区午夜-一区二区三区四区在线视频-一区二区三区四区在线免费观看

服務(wù)器之家:專注于服務(wù)器技術(shù)及軟件下載分享
分類導(dǎo)航

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

服務(wù)器之家 - 服務(wù)器技術(shù) - Nginx - nginx服務(wù)器配置解決ajax的跨域問題

nginx服務(wù)器配置解決ajax的跨域問題

2019-11-25 14:54yoyong Nginx

本文給大家分享的是一個nginx服務(wù)器配置解決ajax的跨域問題的小技巧,非常的實用,有相同需求的小伙伴可以參考下

在采用jquery ajax調(diào)用http請求時,發(fā)現(xiàn)了一系列問題:

如采用firebug調(diào)試API請求(這個API是自己服務(wù)器的應(yīng)用),看到服務(wù)器明明返回200狀態(tài),response返回數(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),因為程序調(diào)用的是遠程服務(wù)器的API,服務(wù)器不允許跨域調(diào)用。如果只是簡單的方法,只需要在程序的response添加支持跨域的header添加屬性”Access-Control-Allow-Origin: *“即可。如java 服務(wù)器代碼:

?
1
2
3
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)容:

?
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';
}
}

 

延伸 · 閱讀

精彩推薦
主站蜘蛛池模板: 亚洲精品高清中文字幕完整版 | 门卫老张和女警花小说 | 青青草国产精品久久碰 | 交换朋友夫妇3中文字幕 | 8x8x拔插 | 2020最新版的ab片 | 九九九九在线精品免费视频 | 国产成人久久精品推最新 | 二次元美女内裤凹陷太深 | 青青青国产手机在线播放 | bbbbbbaaaaaa毛片 | 很黄的网站在线观看 | 超兴奋朋友的中文字幕下 | 91系列在线观看免费 | 久久青草费线频观看国产 | 9久热这里只有精品视频在线观看 | 亚洲热在线视频 | 99re5精品视频在线观看 | 深夜福利影院在线观看 | 亚洲日本va午夜中文字幕 | 午夜性色一区二区三区不卡视频 | 国产中文在线 | 黑人同学征服教师麻麻 | 欧美影院一区二区三区 | 好大好猛好深好爽视频 | 亚洲国产免费 | 欧美日韩在线一区二区三区 | 动漫美丽妇人1~2在线看 | 青青热久免费精品视频精品 | 亚洲精品第五页中文字幕 | 亚洲 小说 欧美 激情 另类 | 青青草国产精品久久碰 | 日韩欧美综合在线二区三区 | 深夜日韩 | 88av视频在线观看 | 国产一区二区三区在线看 | 明星ai智能人脸替换造梦在线播放 | 国产一区二区精品久久91 | 好看华人华人经典play | 国产欧美日韩免费一区二区 | 久久热在线视频精品1 |