nginx解决静态页面中ajax跨域方案配置

nginx 配置内容

server {

listen 9999;#监听端口

server_name localhost;#域名

add_header Access-Control-Allow-Origin *; # 允许跨域

#charset koi8-r;

#access_log logs/host.access.log main;

location / {

root D:/html/index.html;#静态html项目的地址

index index.html;

autoindex off;

}

location /test/ {

#以下为Ajax中跨域配置项

add_header 'Access-Control-Allow-Origin' '*';

add_header 'Access-Control-Allow-Methods' 'GET, POST, OPTIONS';

add_header 'Access-Control-Allow-Headers' '*';

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

proxy_pass http://123.456.789.10:1234;# 测试请求地址为你项目具体请求地址

}

}

nginx 命令:

start nginx 启动nginx

nginx -s stop 快速关闭nginx

nginx -s reload 改变了Nginx相关配置,需要重新加载配置而重载。

html 页面请求

var getUrl='http://127.0.0.1:9999/test/net/testtarget'

$.get(getUrl,param,function(data,status){

console.log(data)

console.log(status)

});

相关推荐
Filotimo_18 小时前
Nginx 的概念
运维·nginx
岁岁种桃花儿1 天前
XMLHttpRequest 从入门到实战:GET/POST 请求完整案例
ajax·okhttp
Shi_haoliu1 天前
SolidTime 在 Rocky Linux 9.5 上的完整部署流程
linux·运维·nginx·postgresql·vue·php·laravel
zhengxianyi5152 天前
vue-cli build, vite build 生产部署刷新或弹窗404,页面空白修复方法
前端·javascript·vue.js·nginx·生产部署
zhengxianyi5152 天前
vite build 发布到nginx二级目录——将yudao-ui-go-view打包、部署到big目录下
vue.js·nginx·vite·前后端分离·打包·ruoyi-vue-pro优化·部署运维
JH30732 天前
openfeign vs nginx 负载均衡对比
运维·nginx·负载均衡
爱吃山竹的大肚肚2 天前
Nginx 最核心和常用的命令
java·运维·服务器·前端·nginx
姜太小白2 天前
【Nginx】鉴权接口通过后,导出或下载接口无响应
运维·网络·nginx
老友@2 天前
Docker Nginx HTTPS 实战:Let’s Encrypt SSL 证书生成与自动续期
运维·nginx·docker·https·证书·ssl
m0_748254663 天前
AJAX 基础实例
前端·ajax·okhttp