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)

});

相关推荐
liangshanbo121518 小时前
面试题:页面需要发起大量 Ajax / HTTP 请求时,如何优化请求性能?
前端·http·ajax
PC2005-cloud19 小时前
Nginx 学习笔记:Server 块配置详解,域名路由与多站点部署实战
nginx
zach20 小时前
Vue/React SPA 打包部署后,子路由刷新 401 未登录问题彻底解决
前端·nginx·next.js
RobinDevNotes20 小时前
JAX 分布式训练,和 PyTorch 有什么不一样
人工智能·深度学习·ajax
小七在进步20 小时前
类和对象(四)
java·javascript·ajax
yume_sibai1 天前
02-Nginx进阶配置完全指南(性能优化 + 安全配置 + 缓存配置 + WebSocket代理)
nginx·安全·性能优化
阿狗童鞋1 天前
Nginx反向代理与负载均衡实战指南
运维·nginx·负载均衡
Lsetea1 天前
OpenSSL verify报error 20:CAfile、fullchain与本地信任链分层排查
nginx·https·ssl证书·openssl·证书链
qq_394562002 天前
【在Linux上升级nginx】
linux·服务器·nginx
冬、不眠的夜2 天前
分享一下 nginx 配置一个简单的 web 网站
nginx