spring mvc跨域(post)--filter方案

import javax.servlet.*;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.InputStream;
import java.util.Properties;

public class CORSFilter implements Filter {

    public void CORSFilter(){

    }

    @Override
    public void doFilter(ServletRequest req, ServletResponse resp, FilterChain chain) throws IOException,ServletException {
        HttpServletResponse httpResp = (HttpServletResponse) resp;
        HttpServletRequest httpReq = (HttpServletRequest) req;

        httpResp.setHeader("Access-Control-Allow-Methods", "GET, POST, PUT, OPTIONS");
        httpResp.setHeader("Access-Control-Allow-Origin", "*");
        httpResp.setHeader("Access-Control-Allow-Credentials","true");
        if (httpReq.getMethod().equalsIgnoreCase("OPTIONS")) {
            httpResp.setHeader("Access-Control-Allow-Headers",
                    httpReq.getHeader("Access-Control-Request-Headers"));
        }
        chain.doFilter(req, resp);
    }

   
    @Override
    public void init(FilterConfig arg0) throws ServletException {
    }

    @Override
    public void destroy() {
    }
}

 

  <filter>
    <filter-name>corsFilter</filter-name>
    <filter-class>com.sung.risk.filter.CORSFilter</filter-class>
  </filter>
  <filter-mapping>
    <filter-name>corsFilter</filter-name>
    <url-pattern>/*</url-pattern>
  </filter-mapping>

 

controller:

    @ResponseBody
    @RequestMapping(value = "/getNewsOfficial")
    public JSONObject getNewsOfficial(HttpServletRequest request){
        JSONObject jsonObject1 = new JSONObject();
        String currentPage = request.getParameter("currentPage");
        String pageSize = request.getParameter("pageSize");
        jsonObject1.put("currentPage",currentPage);
        jsonObject1.put("pageSize",pageSize);
        return  jsonObject1;
    }

 

前端页面:

<!DOCTYPE html>
<html>
<head lang="en">
    <meta charset="UTF-8">
    <title>新闻动态</title>
    <meta content="width=device-width, initial-scale=1.0, maximum-scale=1.0, user-scalable=0" name="viewport">
</head>

<body class="body-blackBg">
</body>
<script src="jquery.min.js"></script>
<script>

    $(document).ready(function () {
    $.ajax({
            type: "POST",
            dataType: "json",
            url: "http://localhost:8080/newsofficial/getNewsOfficial.do",
            data:{currentPage:0, pageSize:5},
            success: function (data) {
                console.log(data);
            },
            error: function (data) {

            }
        });
        $.ajax({
            async: false,
            type: 'POST',
            data:{currentPage:0, pageSize:5},
            dataType: "json",
            url: "http://localhost:8080/newsofficial/getNewsOfficial.do",//请求的action路径
            error: function (msg) {//请求失败处理函数
            },
            success: function (data) { //请求成功后处理函数。
                
            }
        })
    })
    
    
    
</script>
</html>

 

这个方案,前端发送的是非json格式。

json格式其实差不多,留待下篇

转载于:https://www.cnblogs.com/grey-wolf/p/7887424.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值