私はSpring MVC上で簡単なJavaアプリケーションを持っており、Springのコントローラにajaxリクエストを送信します。私は、ヘッダーは、 "同意する" "アプリケーション/ JSON"と"のContent-Type"、 "アプリケーション/ jsonの;のcharset = UTF-8" に設定するとAJAX呼び出しで私はときエラーdubuggerで400を取得します私はそれを削除するエラー415を取得します。Spring 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\"}";
}
広告クラスのプロパティを指定してください。 –