美文网首页
Jquery Ajax 跨域 SpringMVC的设置

Jquery Ajax 跨域 SpringMVC的设置

作者: Yuri1996 | 来源:发表于2019-05-23 13:11 被阅读0次

直接上代码

Java

    @ResponseBody
    @RequestMapping(value = "/statistics", method = RequestMethod.GET, produces = { "application/json;charset=UTF-8" })
    public Map<String,Object> getShipPrediction(@RequestParam(value = "date",required=false) String date,ServletRequest req, ServletResponse res) {
        
        HttpServletRequest request = (HttpServletRequest)req;
        HttpServletResponse response = (HttpServletResponse) res;
        String originHeader = request.getHeader("Origin");
        
        // 允许哪些Origin发起跨域请求,nginx下正常
        // response.setHeader( "Access-Control-Allow-Origin", config.getInitParameter( "AccessControlAllowOrigin" ) );
        response.setHeader( "Access-Control-Allow-Origin", "*" );
        // 允许请求的方法
        response.setHeader( "Access-Control-Allow-Methods", "POST,GET,OPTIONS,DELETE,PUT" );
        // 多少秒内,不需要再发送预检验请求,可以缓存该结果
        response.setHeader( "Access-Control-Max-Age", "3600" );
        // 表明它允许跨域请求包含xxx头
        response.setHeader( "Access-Control-Allow-Headers", "x-auth-token,Origin,Access-Token,X-Requested-With,Content-Type, Accept,Authorization" );
        //response.setHeader("Access-Control-Allow-Headers", "Origin, No-Cache, X-Requested-With, If-Modified-Since, Pragma, Last-Modified, Cache-Control, Expires, Content-Type, X-E4M-With,userId,token");  
        //是否允许浏览器携带用户身份信息(cookie)
        response.setHeader( "Access-Control-Allow-Credentials", "true" );
        // response.setHeader( "Access-Control-Expose-Headers", "*" );
        
        return shipPredictionStatisticsService.returnShipPredictionMap(date);
    }

JavaScript

$(document).ready(function(){
    //发起http请求
    $.ajax({
        headers: {
            Accept: "application/json; charset=utf-8",
            Authorization: window.localStorage.Authorization
        },
        url: "http://localhost:8888/menuListById",
        type: "get",
        data:{
            qqq:'222' 
        },
        success: function (data) {
            console.log(data)
        },
        fail:function(e){
        }
    });

})

PS:后台或者使用过滤器(一次性解决问题)

package filter;

import java.io.IOException;
 
import javax.servlet.Filter;
import javax.servlet.FilterChain;
import javax.servlet.FilterConfig;
import javax.servlet.ServletException;
import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
 
/**
 * 头部过滤器
 * @author 
 */
public class HeaderFilter implements Filter{
 
    public void destroy() {
        
    }
 
    public void doFilter(ServletRequest req, ServletResponse res, FilterChain chain)
            throws IOException, ServletException {
        HttpServletRequest request = (HttpServletRequest)req;
        HttpServletResponse response = (HttpServletResponse) res;
        String originHeader = request.getHeader("Origin");
        
        // 允许哪些Origin发起跨域请求,nginx下正常
        // response.setHeader( "Access-Control-Allow-Origin", config.getInitParameter( "AccessControlAllowOrigin" ) );
        response.setHeader( "Access-Control-Allow-Origin", "*" );
        // 允许请求的方法
        response.setHeader( "Access-Control-Allow-Methods", "POST,GET,OPTIONS,DELETE,PUT" );
        // 多少秒内,不需要再发送预检验请求,可以缓存该结果
        response.setHeader( "Access-Control-Max-Age", "3600" );
        // 表明它允许跨域请求包含xxx头
        response.setHeader( "Access-Control-Allow-Headers", "x-auth-token,Origin,Access-Token,X-Requested-With,Content-Type, Accept,Authorization" );
        //response.setHeader("Access-Control-Allow-Headers", "Origin, No-Cache, X-Requested-With, If-Modified-Since, Pragma, Last-Modified, Cache-Control, Expires, Content-Type, X-E4M-With,userId,token");  
        //是否允许浏览器携带用户身份信息(cookie)
        response.setHeader( "Access-Control-Allow-Credentials", "true" );
        // response.setHeader( "Access-Control-Expose-Headers", "*" );
        if (request.getMethod().equals( "OPTIONS" )) {
            response.setStatus( 200 );
            return;
        }
        chain.doFilter(request, response);  
    }
 
    public void init(FilterConfig arg0) throws ServletException {
        
    }
}

相关文章

  • Jquery Ajax 跨域 SpringMVC的设置

    直接上代码 Java JavaScript PS:后台或者使用过滤器(一次性解决问题)

  • ajax、fetch 跨域携带cookie

    一、ajax 跨域携带cookie 原生ajax请求方式: jquery的post方法请求: 服务器端设置: 二、...

  • ajax跨域请求

    ajax跨域请求(jsonp) 利用JSONP解决AJAX跨域问题的原理与jQuery解决方案JSONP jQue...

  • Ajax跨域

    CORS Ajax跨域不携带COOKIE PHP jQuery

  • SpringMVC 进行ajax跨域请求访问

    关于 springmvc 3.x 版本对ajax跨域请求访问 ajax 请求后,浏览器出现跨域的问题那么在当前环境...

  • 跨域实战解决方案

    一.跨域方案 1.JSONP跨域 (1)前端发起jQuery ajax 的get请求 $.getJSON...

  • jquery ajax 跨域

    jquery ajax 跨域 客户端“跨域访问”一直是一个头疼的问题,好在有jQuery帮忙,从jQuery-1....

  • 跨域上传图片并预览

    跨域上传文件 之前解决跨域问题都是使用的JQuery的JSONP类型的Ajax请求,JSONP原理是让服务器把数据...

  • jQuery笔记

    Ajax跨域访问 dataType设置为"jsonp" jsonp设置为"jsonpCallback"该参数是用于...

  • 解决ajax跨域问题

    Jsonp解决ajax跨域问题 CORS解决ajax跨域问题

网友评论

      本文标题:Jquery Ajax 跨域 SpringMVC的设置

      本文链接:https://www.haomeiwen.com/subject/sguvzqtx.html