2016-11-24 137 views
0

我在Spring MVC上有一个简单的Java应用程序,并向Spring控制器发送ajax请求。当我设置头“接受”, “应用/ JSON”“内容类型”, “应用/ JSON的;字符集= UTF-8”在AJAX调用我得到错误时400在dubugger,我删除它我得到错误415Spring MVC + Ajax错误400

如果我改变控制器方法签名公共字符串logoutPage(@RequestBody字符串obyavleniye)我得到JSON字符串。在控制器中解析请求会有什么问题?

JS方法:

$("#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); 
      } 
     }); 
    }); 

控制器方法:

@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\"}"; 
    } 
+0

请指定广告类的属性。 –

回答

1

我的样本代码。

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) { 

      } 
     }); 
    }; 

控制器

@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"; 
    } 
+0

和哪里有区别?我不明白为什么我会得到错误。 – Dmitry

+1

您在“接受”,“应用程序/ json”中使用HTTP标头。那么你的控制器取(产生=“application/json”)或使用“contentType”,“application/json”。那么你的控制器需要(消耗=“application/json”)。但你使用两种情况。 – Byeon0gam

+1

而你在js中使用dataType:'json'。你的控制器返回responseType。你使用dataType:'text'和var data = eval('('+ response +')');或者如果使用'json'。你可以搜索dataType:'json'和服务器代码。它很长的评论。 – Byeon0gam

1

执行以下步骤:

1)尽量保持杰克逊的jar文件在类路径

2)eighter你必须在发送AJAX请求即删除的数据类型,

dataType : "json" 

或者必须产生应用/ JSON响应如下

@RequestMapping(value = "/upload", method = RequestMethod.POST, produces = MediaType.APPLICATION_JSON_UTF8_VALUE) 

3)检查DTO或广告类的属性,其类型应与传入的请求匹配。即请求参数应与DTO成员的姓名和类型相匹配。

这些是可能的方法来避免你的情况。