【关于 postJson 请求】实操和视频中有出入,不知道原因...求解

来源:5-10 简单请求和非简单请求1

Hemingway_AT

2019-12-06 17:14

一、代码

调用方:ajax发送post请求,带请求体

$.ajax({
    type: "post",
    url: base + "/postJson",
    contentType: "application/json;charset=utf-8",
    data: JSON.stringify({name: "Hemingway"}),
    success: function (json) {
        result = json;
    }
});

被调用方:在简单 get 请求过滤器配置的基础上,未做任何操作

    @Override
    public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain)
            throws IOException, ServletException {
        // TODO Auto-generated method stub

        HttpServletResponse res = (HttpServletResponse) response;
        HttpServletRequest req = (HttpServletRequest) request;

        String origin = req.getHeader("Origin");
        if (!org.springframework.util.StringUtils.isEmpty(origin)) {
            //带cookie的时候,origin必须是全匹配,不能使用*
            res.addHeader("Access-Control-Allow-Origin", origin);
        }

        chain.doFilter(request, response);
    }

二、测试表现

network 面板 》》

http://img1.mukewang.com/5de9fdcf0001183404560234.jpg

console 面板 》》


http://img4.mukewang.com/5de9fe4200012ff308030084.jpg

三、疑问

1、network 显示一次 xhr 请求类型,没有出现 option;

2、“It does not have HTTP ok status.”一直没找到解决方案。

1)postman直接测rest接口,是通过的:

http://img3.mukewang.com/5dea1bf10001be9306500444.jpg

2)后台产生了异常日志

http://img2.mukewang.com/5dea1bca00013f9b05930090.jpg


写回答 关注

1回答

  • 慕梦前来
    2020-03-09 12:57:30

    400 了,这肯定是路径不对了,你先对一下请求路径吧

ajax跨域完全讲解

完全讲解Ajax跨域产生的原因和解决思路,掌握跨域的各种解决方案

62566 学习 · 90 问题

查看课程

相似问题