后端代码:
//ajax请求的跨域允许
		resp.setHeader("Access-Control-Allow-Credentials","true"); //是否支持cookie跨域
		resp.setHeader("Access-Control-Allow-Origin", "http://localhost:8080");
    第二句中必须指定地址,不能用”*”来代替,而且127.0.0.1和localhost之间也算跨域,要注意这个坑
    
    前端代码:
   
$.ajax({
				url: "http://localhost:8080/Servlet/MyServlet",
				dataType:"text",
				type:"get",
	            xhrFields: {
	                withCredentials: true
	            },
	            crossDomain: true,
	            success:function(result){
	            	console.log(result)
	            }
			});
    
     前端代码中一定要加上
    
    
    xhrFields: {
    
    
    withCredentials: true
    
    },
    
    crossDomain: true,
    
    
     这两句话
    
   
 
版权声明:本文为qq_41079359原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接和本声明。
