java Spring MVC Ajax POST
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/35612821/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Spring MVC Ajax POST
提问by Rapidistul
I'm trying to post a JSON Object, with Ajax, to an Rest Spring MVC Controller, but I met some troubles.
我正在尝试使用 Ajax 将 JSON 对象发布到 Rest Spring MVC 控制器,但遇到了一些麻烦。
Let's present you my code:
让我们向您展示我的代码:
Controller
控制器
@RequestMapping(value = "/register/checkUsername.html", method = RequestMethod.POST, produces=MediaType.APPLICATION_JSON_VALUE)
public @ResponseBody JsonResponse checkUsername(@RequestBody JsonUsername username) {
String usernameString = username.getUsername();
JsonResponse response = new JsonResponse();
response.setMessage(usernameString + "Available");
return response;
}
Ajax Function
Ajax 函数
function displayUsernamError(data) {
var json = "<h4>Eroare</h4><pre>"
+ JSON.stringify(data, null, 4) + "</pre>";
$('#usernameError').html(json);
}
function checkUsername(){
var username = document.getElementById("username");
var search = {
"username" : username.value
}
$.ajax({
type : "POST",
contentType : 'application/json; charset=utf-8',
dataType : 'json',
url : "http://localhost:8080/skill-assessment/register/checkUsername.html",
data : JSON.stringify(search),
success : function(result) {
console.log("SUCCESS: ", data);
displayUsernamError(result);
},
error: function(e){
console.log("ERROR: ", e);
displayUsernamError(e);
},
done : function(e) {
console.log("DONE");
}
});
}
And finally the html form:
最后是 html 表单:
<form:form modelAttribute="user" method="POST" enctype="utf8">
<tr>
<td><label>Username</label></td>
<td><form:input path="name" id="username" /></td>
<td>
<p id="usernameError">
</p>
</td>
</tr>
<button type="button" onClick="checkUsername()">Register</button>
</form:form>
So, I'm calling the checkUsername method when I press click on the Register button, only for test.
所以,当我点击注册按钮时,我正在调用 checkUsername 方法,仅用于测试。
The ajax function is called and this send the JSON to the controller. I used the debug and the controller seems to get the JSON Object.
调用 ajax 函数并将 JSON 发送到控制器。我使用了调试,控制器似乎得到了 JSON 对象。
The problem is with the controller Response, because in the error div from my html page, I get this error:
问题出在控制器响应上,因为在我的 html 页面的错误 div 中,我收到此错误:
HTTP Status 406 The resource identified by this request is only capable of generating responses with characteristics not acceptable according to the request \"accept\" headers.
HTTP 状态 406 此请求标识的资源只能生成具有根据请求“接受”标头不可接受的特征的响应。
Where is the problem? Who can give me an idea?
哪里有问题?谁能给我出个主意?
采纳答案by Nalla Srinivas
You need to specify the below code inside of the request mapping annotation
您需要在请求映射注释中指定以下代码
@RequestMapping(value = "/register/checkUsername.html", method = RequestMethod.POST, consumes=MediaType.APPLICATION_JSON_VALUE)
you no need to specify the produces attribute because @Responsebody will produce the content based on the request object content type[optional]. if you want other then it then only you have to mention it.
您无需指定生产属性,因为@Responsebody 将根据请求对象内容类型[可选] 生成内容。如果你想要其他然后它那么你只需要提到它。
; charset=utf-8 is the default one in Ajax call. you no need mention it separately. try do these changes.
; charset=utf-8 是 Ajax 调用中的默认值。您无需单独提及。尝试做这些改变。
回答by lucky kurniawan
in .js(java script) ajax function
在 .js(java script) ajax 函数中
var variable="test";
$.ajax({
url: baseUrl + "nameController/test1",
async: false,
data: {val: variable},
dataType: 'html',
success: function (dat) {
console.log(dat);
}
});
you create nameController.java controller
你创建 nameController.java 控制器
@RequestMapping(value = "test1", method = RequestMethod.POST)
public @ResponseBody
String checkRoomStatusReservation(@RequestParam(value = "val", required = true) String parse) {
System.out.println("parse"+parse);
//value from parse=test
return parse;
}
you can try this
你可以试试这个
回答by qiang.wei.kang
use @ResponseBody
need object to json,
seems missing some jar file
(Hymanson-mapper-asl.jar and Hymanson-core-asl.jar)
try to import it
使用@ResponseBody
需要对象到 json,似乎缺少一些 jar 文件(Hymanson-mapper-asl.jar 和 Hymanson-core-asl.jar)尝试导入它
回答by Rima
First check if your controller is returning a JSON. You can use any REST client and see what is the return header. If not then add the Hymanson mapper bean.
首先检查您的控制器是否返回 JSON。您可以使用任何 REST 客户端并查看返回标头是什么。如果没有,则添加 Hymanson mapper bean。
Also, try setting the Accept header in the ajax call.
此外,尝试在 ajax 调用中设置 Accept 标头。
$.ajax({
headers: {
Accept : "application/json",
Content-Type: "application/json"
},
data: "data",
...
})