麻豆小视频在线观看_中文黄色一级片_久久久成人精品_成片免费观看视频大全_午夜精品久久久久久久99热浪潮_成人一区二区三区四区

首頁 > 網(wǎng)站 > Nginx > 正文

nginx服務器配置解決ajax的跨域問題

2024-08-30 12:28:45
字體:
供稿:網(wǎng)友

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

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

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配置文件添加一下內(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';}}


發(fā)表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發(fā)表
主站蜘蛛池模板: 久久久久久久一区二区三区 | 最新av在线免费观看 | 欧美亚洲一级 | 国产成人精品免费视频大全办公室 | 国产xxxx免费 | 欧美亚洲一级 | 蜜桃传媒视频麻豆第一区免费观看 | 黄色av电影在线 | 成人短视频在线播放 | 成人毛片免费 | 久久久精品视频免费 | 黄污网站在线观看 | 狠狠操夜夜爱 | 国产伦精品一区二区三区在线 | 国产啊v在线观看 | 麻豆视频在线观看 | 看免费5xxaaa毛片| 色污视频在线观看 | 免费视频www在线观看 | 久久久国产一级片 | 亚洲一区二区免费 | 91网站链接 | 美女视频大全网站免费 | 欧美一级精品片在线看 | 中文字幕观看 | 中国性xxx| 久久精品com | 欧美日韩爱爱视频 | 精品国产一区二区亚洲人成毛片 | 欧美一级二级毛片视频 | 一级片久久免费 | 韩国精品视频在线观看 | 在线看一区二区三区 | 成人三级黄色片 | 免费毛片电影 | 九九精品在线播放 | 中文字幕 亚洲一区 | 欧洲成人综合网 | 91av在线免费播放 | xxxxxx中国| 欧美激情性色生活片在线观看 |