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)

});

相关推荐
是良辰6 小时前
Docker Nginx HTTPS 自签证书部署完整操作文档
nginx·docker·https
難釋懷10 小时前
Nginx外置缓存-redis2-nginx-module
nginx·缓存·junit
凡尘——雨落凡尘1 天前
PHP 线上十大隐形故障复盘:90% 的网站卡顿、502、雪崩,都是这些细节导致的
redis·nginx·php·session
Deryck_德瑞克1 天前
【Nginx】配置差异分析
服务器·前端·nginx
学者猫头鹰1 天前
Nginx零基础学习手册
nginx
難釋懷2 天前
Nginx外置缓存-nginx + memcached
nginx·缓存·memcached
難釋懷2 天前
Nginx外置缓存-nginx + redis
redis·nginx·缓存
starzy19902 天前
Spark 核心之 Driver 原理剖析
大数据·ajax·spark
☆凡尘清心☆3 天前
CentOS Stream 9 专用 LNMP 全自动一键脚本
linux·运维·mysql·nginx·lnmp·centos stream 9
似的8354 天前
一步一步学习使用FireMonkey动画() 使用TAnimator类创建动画
linux·学习·nginx