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)

});

相关推荐
MACKEI6 分钟前
服务器流式传输接口问题排查与解决方案
python·nginx·流式
曹牧2 小时前
Nginx:正向代理与反向代理
运维·nginx
莫大33014 小时前
2核2G云服务器PHP8.5+MySQL9.0+Nginx(LNMP)安装WordPress网站详细教程
运维·服务器·nginx
银发控、15 小时前
nginx静态资源
运维·nginx
2601_9498095919 小时前
flutter_for_openharmony家庭相册app实战+相册详情实现
javascript·flutter·ajax
熊猫钓鱼>_>1 天前
动态网站发布部署核心问题详解
前端·nginx·容器化·网页开发·云服务器·静态部署
Swift社区1 天前
Nginx 反向代理配置 React 前端与 Python 后端
前端·nginx·react.js
Dontla1 天前
Kubernetes流量管理双雄:Ingress与Gateway API解析(Nginx与Ingress与Gateway API的关系)
nginx·kubernetes·gateway
lcx_defender1 天前
【Docker】Docker部署运行Nginx
nginx·docker·容器
可可嘻嘻大老虎1 天前
nginx无法访问后端服务问题
运维·nginx