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)

});

相关推荐
泡沫冰@40 分钟前
上章节中文件的讲解
前端·网络·nginx
難釋懷7 小时前
Nginx内存缓存
nginx·缓存·junit
云计算磊哥@11 小时前
运维开发宝典059-大型网站nginx服务器管理全集5
服务器·nginx·运维开发
cesium vue1 天前
nginx 流媒体配置
运维·nginx
zhougl9961 天前
Gateway 和 Nginx 路由区别
运维·nginx·gateway
布兰妮甜1 天前
跨域全方案对比:CORS、Nginx 反向代理、JSONP、iframe、postMessage
nginx·跨域·cors·前端架构·浏览器安全
難釋懷1 天前
Nginx-proxy缓存断点续传缓存 range
运维·nginx·缓存
BD_Marathon1 天前
Spark数据源
大数据·ajax·spark
難釋懷1 天前
Nginx-proxy缓存清理
运维·nginx·缓存
用户0780625347192 天前
getUserMedia 权限排障实战:浏览器拒绝,不一定是用户点了“拒绝”
nginx·webrtc