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)

});

相关推荐
lv__pf3 小时前
ajax的使用、json、jquery
ajax·json·jquery
翔云12345611 小时前
在nginx中,proxy_next_upstream off是什么含义
nginx
虎王物联11 小时前
Nginx反向代理实现IoT设备HTTPS接入:从证书配置到MQTT over WebSocket实战
运维·物联网·nginx·https
Linux-lucky1 天前
28-Linux学习之旅之Maven与Nexus制品库
linux·运维·学习·nginx·tomcat·maven
小波波啊1 天前
nginx 转发超时故障排查实录
运维·nginx
陈皮波比茶1 天前
Nginx
nginx
mengge.cloud1 天前
0824LAMP项目实战:部署WordPress博客平台小白教程
android·linux·运维·服务器·学习·nginx
用户104256177361 天前
Nginx生产级配置优化指南
nginx
豆角焖肉1 天前
Layui+Layer 弹出层实现 CRUD:AJAX 无刷新新增修改删除后台实战
前端·ajax·layui
渣渣盟1 天前
Nginx 从零到上手:Windows & Linux 双环境教程
linux·windows·nginx