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)

});

相关推荐
百***67035 小时前
Nginx搭建负载均衡
运维·nginx·负载均衡
遥遥晚风点点6 小时前
Spark导出数据文件到HDFS
前端·javascript·ajax
m0_5695310110 小时前
Nginx(4)--Nginx与tomcat反向代理和负载均衡
nginx·tomcat·负载均衡
R.lin14 小时前
浅谈Nginx
运维·nginx
Crazy________15 小时前
37负载均衡介绍和nginx模块编译安装
运维·nginx·负载均衡
百***341316 小时前
Nginx实现接口复制
运维·nginx·junit
xhxxx17 小时前
《大厂面试:从手写 Ajax 到封装 getJSON,再到理解 Promise 与 sleep》
ajax·面试
天黑请闭眼19 小时前
华为对象存储:nginx代理临时访问地址后访问报错:Authentication Failed
nginx·华为
执念WRD1 天前
熊海CMS v1.0代码审计实战
android·nginx·安全·web安全·网络安全·系统安全
q***51891 天前
离线安装 Nginx
运维·数据库·nginx