weixin_33725239 2016-11-24 18:31 采纳率: 0%
浏览 49

Spring MVC + Ajax错误400

I have a simple java application on Spring MVC and I send ajax request to Spring controller. When I set headers "Accept", "application/json" and "Content-Type", "application/json;charset=utf-8" in AJAX call I get error 400 in dubugger, when I delete it I get error 415.

If I change controller method signature to public String logoutPage (@RequestBody String obyavleniye) I get JSON string. What problem can be with parse request in controller?

JS method:

$("#advertForm").submit(function(e) {
        e.preventDefault();
        var token = $("meta[name='_csrf']").attr("content");
        var header = $("meta[name='_csrf_header']").attr("content");
        var obyavleniye = {
            title: "Title",
            price: "80",
            description: "desc",
            date: "2016-11-07 18:30:21",
            authorid: "2",
            category: "A",
            state: "new",
            img1: "http",
            img2: "http",
            img3: "http",
            img4: "http",
        };
        var post_data = JSON.stringify(obyavleniye);

        console.log(post_data);
        $.ajax({
            url : "/upload",
            type: "POST",
            dataType: 'json',
            data: post_data,
            beforeSend: function(xhr) {
                xhr.setRequestHeader("Accept", "application/json");
                xhr.setRequestHeader("Content-Type", "application/json;charset=utf-8");
                xhr.setRequestHeader(header, token);
            },
            complete: function() {
                console.log("Sent");
            },
            success: function (response) {
                console.log("success");
                console.log("response" + response);
            },
            error: function (data) {
                console.log("error");
                console.log(data);
            }
        });
    });

Controller method:

@ResponseBody
    @RequestMapping(value="/upload", method = RequestMethod.POST)
    public String logoutPage (@RequestBody Advert obyavleniye) {
//    public String logoutPage (@RequestBody String obyavleniye) {
        System.out.println("Enter: " + obyavleniye);
        this.advertService.addAdvert(obyavleniye);
//        return "{\"msg\":\"success\"}";
        return "{\"title\":\"Title\",\"price\":\"80\",\"description\":\"normm\",\"date\":\"2016-11-07 18:30:21\",\"authorid\":\"2\",\"category\":\"A\",\"state\":\"new\",\"img1\":\"http\",\"img2\":\"http\",\"img3\":\"http\",\"img4\":\"http\"}";
    }
  • 写回答

2条回答 默认 最新

  • weixin_33698823 2016-11-25 07:39
    关注

    My sample code.

    js

      Company.prototype.saveCompanyLocation = function() {
            /* company */
            var companyIdx = $('#companyIdx').val();
            var locationIdx = $('#locationIdx').val();
    
            var data = {
                idx : locationIdx,
    
                postCode : $('#postCode').val(),
                address : $('#address').val(),
                detailAddress : $('#detailAddress').val(),
    
                tel : $('#tel').val(),
                fax : $('#fax').val(),
                email : $('#email').val(),
                language : $("#language").val(),
    
                latitude : $('#latitude').val(),
                longtitude : $('#longtitude').val()
    
            };
    
            data = JSON.stringify(data);
    
            $.ajax({
                url : "/gpim/company/settings/location/save/" + companyIdx,
                type : 'POST',
                data : data,
                contentType : 'application/json',
    
                success : function(response) {
                    if (response == "success") {
                        document.location.reload(true);
                    } else {
                        $("#editMsg").text("you can`t save location information.");
                    }
                },
                error : function(request, status, error) {
    
                }
            });
        };
    

    controller

    @RequestMapping(value = "/settings/location/save/{companyIdx}", method = RequestMethod.POST)
        public @ResponseBody String saveLocation(@PathVariable int companyIdx, @RequestBody CompanyLocation location) {
            Company company = companyService.findCompanyByIdx(companyIdx);
    
            company = companyService.saveCompanyLocation(company, location);
    
            if (company != null) {
                return "success";
            }
    
            return "fail";
        }
    
    评论

报告相同问题?

悬赏问题

  • ¥15 MATLAB动图的问题
  • ¥15 求差集那个函数有问题,有无佬可以解决
  • ¥15 【提问】基于Invest的水源涵养
  • ¥20 微信网友居然可以通过vx号找到我绑的手机号
  • ¥15 寻一个支付宝扫码远程授权登录的软件助手app
  • ¥15 解riccati方程组
  • ¥15 display:none;样式在嵌套结构中的已设置了display样式的元素上不起作用?
  • ¥15 使用rabbitMQ 消息队列作为url源进行多线程爬取时,总有几个url没有处理的问题。
  • ¥15 Ubuntu在安装序列比对软件STAR时出现报错如何解决
  • ¥50 树莓派安卓APK系统签名