html이 ajax를 사용하여 백그라운드 인터페이스에 액세스할 때 교차 도메인 문제가 있습니다.

로컬 html이 ajax를 사용하여 백그라운드 인터페이스에 액세스할 때 교차 도메인 문제가 있습니다.

场景描述:本地html项目使用ajax发送请求出现如下报错:

여기에 이미지 설명 삽입

해결책:

  1. 백엔드 솔루션: 예제 링크

    具体方式:
    a. 使用Filter方式进行设置
    b. 继承 HandlerInterceptorAdapter
    c. 实现 WebMvcConfigurer
    d. 使用 @CrossOrgin 注解 --推荐使用
    e. 网关配置
    
  2. nginx 구성

    配置如下:
    server {
    	listen 前端访问端口号;
    	server_name localhost;
    	
    	location / {
    		root   本地项目目录;
            index  访问页.html 访问页.htm;
    	}
    	# 配置代理,location后面的路径是匹配前端访问地址的
    	location /api {
    		proxy_pass  http://localhost:后端服务端口;
    	}
    
    }
    

프런트 엔드 액세스의 예:

$.ajax({
    
    
		url: "/api/test/tt",
		type: "post",
        useDefaultXhrHeader: true,
		dataType: "json",
		data:{
    
    },
		success: function (data) {
    
    
			console.log(data);
        }
	})

추천

출처blog.csdn.net/Shiny_boy_/article/details/126020542