JQuery CDN,Ajax,java跨域请求问题

网页:包含一个CDN和一个post

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
</head>
<script src="https://cdn.bootcss.com/jquery/3.2.1/jquery.min.js"></script>
<script>
    function search() {
        $.post({
            url:'http://localhost:8080/onlineoj/test/update',
            data: {
                'id': $('#id').val(),
                'username': $('#username').val(),
                'password': $('#password').val()
            },
            success:function (data, status){
                alert(data);
                alert(status);
            }
        })
    // alert('xxx');
        return false;
    }
</script>
<body>
    <form>
        <input type="text" id="id" />
        <input type="text" id="username" />
        <input type="text" id="password" />
        <button type="submit" onclick="return search();">查询</button>
    </form>
</body>
</html>

Cros请求

package com.onlineoj.interceptor;

import org.springframework.context.annotation.Configuration;

import javax.servlet.*;
import javax.servlet.annotation.WebFilter;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

/**
 * 跨域请求
 */
@WebFilter(filterName = "crosFilter", urlPatterns = "/*")
@Configuration
public class CrosFilter implements Filter {
    @Override
    public void init(FilterConfig filterConfig) throws ServletException {

    }

    @Override
    public void doFilter(ServletRequest req, ServletResponse resp, FilterChain chain) throws IOException, ServletException {
        //设置跨域请求
        HttpServletResponse response = (HttpServletResponse) resp;
        //此处ip地址为需要访问服务器的ip及端口号
        response.setHeader("Access-Control-Allow-Origin", "*");
        response.setHeader("Access-Control-Allow-Credentials", "true");
        response.setHeader("Access-Control-Allow-Methods", "GET, HEAD, POST, PUT, DELETE, TRACE, OPTIONS, PATCH");
        response.setHeader("Access-Control-Allow-Headers", "Origin, X-Requested-With, Content-Type,Token,Accept, Connection, User-Agent, Cookie");
        response.setHeader("Access-Control-Max-Age", "3628800");
        chain.doFilter(req, resp);
    }

    @Override
    public void destroy() {

    }
}

 

posted @ 2020-02-16 18:00  SteveYu  阅读(604)  评论(0编辑  收藏  举报