1.本文静态文件存在本地目录,其余数据由远程服务提供
2.Nginx配置如下
worker_processes 1;
events {
worker_connections 1024;
}
http {
include mime.types;
default_type application/octet-stream;
sendfile on;
server {
listen 80;
server_name 192.168.98.128:8080;
#静态文件存在桌面
location ~ \.(html|js|css|png|gif)$ {
root C:/Users/jh/Desktop;
}
#匹配demo开头的url,服务由远程的spring-boot提供
location /demo/ {
proxy_pass http://192.168.98.128:8080/demo/;
}
}
}
3.在nginx目录下启动nginx,start nginx(关闭服务 命令,nginx -s stop)
4.访问如下
浏览器访问 http://localhost/test.html
访问http://localhost/demo/emp/name
5.test.html文件如下
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<script src="https://cdn.bootcss.com/jquery/1.10.2/jquery.min.js">
</script>
<script>
$(document).ready(function(){
$("button").click(function(){
alert("hello world");
$.get("./demo/emp/name",function(data,status){
alert("数据: " + data + "\n状态: " + status);
$("#test3").val(data);
});
});
});
</script>
</head>
<body>
<button>HTTP GET</button>
<input type="text" id="test3" value="hello world"></p>
</body>
</html>
点击按钮,能够获取后台数据,实现跨域