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)

});

相关推荐
Debug 熊猫20 小时前
Nginx代理快速入门(结合vue3简单项目讲解)
运维·nginx
兔子零102421 小时前
nginx 配置长跑(下):全局变量、调试思路与可观测性
后端·nginx
小小哭包1 天前
Nginx配置文件nginx.conf中文详解
运维·nginx
weixin_431697201 天前
onlyoffice预览nginx代理的静态文件
运维·nginx
6***A6631 天前
Nginx 反向代理配置
运维·nginx
pcc_is_world2 天前
Nginx HTTPS服务器搭建与认证配置
服务器·nginx·https
wadesir2 天前
Nginx反向代理错误处理(从零开始配置自定义错误页面与故障应对)
运维·nginx
chilavert3182 天前
技术演进中的开发沉思-228 Ajax: Aptana开发
前端·javascript·ajax
士心凡2 天前
Spark
大数据·ajax·spark
半路_出家ren2 天前
LNMP环境与应用配置
linux·数据库·mysql·nginx·网络安全·php·lnmp