nginx服务器通过配置来解决API的跨域问题

更新时间:2023-06-22 07:08:12 阅读: 评论:0

nginx服务器通过配置来解决API的跨域问题
前⾔
最近在采⽤jquery ajax调⽤http请求时,发现了⼀系列问题:relative是什么意思
如采⽤firebug调试API请求(这个API是⾃⼰服务器的应⽤),看到服务器明明返回200状态,respon返回数据也是json格式,但ajax返回的error。
在排除json数据格式不正确的原因之后,发现了ajax error函数返回“networkerror failed to execute ‘nd' on ‘xmlhttprequest' failed to load ‘http //“XMLHttpRequest.status=0,就是没有初始化。
后来才知道是跨域问题(CORS),因为程序调⽤的是远程服务器的API,服务器不允许跨域调⽤。如果只是简单的⽅法,只需要在程序的respon 添加⽀持跨域的header添加属性”Access-Control-Allow-Origin: * “即可。芦荟怎么养
如java 服务器代码:
yourownvariable.tHeader("Access-Control-Allow-Origin:", "origin url of your site");
yourownvariable.tHeader("Access-Control-Allow-Methods", "GET, POST,PUT");
如果是配置nginx服务器(如果是其他服务器,可以参考:),需要在f配置⽂件添加⼀下内容:
#
# 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 browrs *should* be OK with but aren't
拜年歌#
add_header 'Access-Control-Allow-Headers' 'DNT,X-CustomHeader,Keep-Alive,Ur-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 chart=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,Ur-Agent,X-Requested-With,If-Modified-Since,Cache-Control,Content-Type,Content-Range,Range'; add_header 'Access-Control-Expo-Headers' 'DNT,X-CustomHeader,Keep-Alive,Ur-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,Ur-Agent,X-Requested-With,If-Modified-Since,Cache-Control,Content-Type,Content-Range,Range'; add_header 'Access-Control-Expo-Headers' 'DNT,X-CustomHeader,Keep-Alive,Ur-Agent,X-Requested-With,If-Modified-Since,Cache-Control,Content-Type,Content-Range,Range'; }
}
总结司马彦字帖
以上就是这篇⽂章的全部内容了,希望本⽂的内容对⼤家的学习或者⼯作能带来⼀定的帮助,如果有疑问⼤家可以留⾔交流,谢谢⼤家对的⽀持。

本文发布于:2023-06-22 07:08:12,感谢您对本站的认可!

本文链接:https://www.wtabcd.cn/fanwen/fan/82/1011683.html

版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。

标签:服务器   跨域   返回   添加   问题
相关文章
留言与评论(共有 0 条评论)
   
验证码:
推荐文章
排行榜
Copyright ©2019-2022 Comsenz Inc.Powered by © 专利检索| 网站地图