小白请教个问题, ajaxq 请求总是报 400 错误

2018-04-11 00:20:04 +08:00
 haikea
贴上代码
---------------------后端代码-----------------------
@RequestMapping(value="/testJson")
@ResponseBody
public TestJsonClass testJson(@RequestBody TestJsonClass testJsonClass){
System.out.println("yyy");
System.out.println(testJsonClass.getName());
TestJsonClass jjj=new TestJsonClass("jjj");
return jjj;

}
-------------------------TestJsonClass 类-----------------------
public class TestJsonClass {

private String name;

public String getName() {
return name;
}

public void setName(String name) {
this.name = name;
}

public TestJsonClass(String name) {
super();
this.name = name;
}
}
----------------------------前端代码-------------------------------
<!-- json&ajax 测试 -->
<br><br>
<form id="myForm">
链接:<input type="text" id="name" name="name"/>
<br>
<input type="button" id="select" value="提交"/>
</form>
<script type="text/javascript">
$("#select").click(function() {
$.ajax({
url : "testJson",
type : "POST",
contentType : "application/json;charset=UTF-8",
"data": '{"name": "uname"}',
success:function(result) {
alert(result.name);
}
});
});
</script>
1709 次点击
所在节点    问与答
4 条回复
haikea
2018-04-11 00:21:44 +08:00
各位大神能帮忙看看是什么问题么
Mazexal
2018-04-11 09:49:34 +08:00
HTTP 错误 400
400 请求出错
由于语法格式有误,服务器无法理解此请求。不作修改,客户程序就无法重复此请求。
q253382683
2018-04-11 09:54:48 +08:00
你这格式。。。
quartet
2018-04-11 18:02:05 +08:00
$.ajax({
url : "testJson",
type : "POST",
contentType : "application/json;charset=UTF-8",
dataType : "json",
data: JSON.stringify({"name": "uname"}),
success:function(result) {
alert(result.name);
}
});

这是一个专为移动设备优化的页面(即为了让你能够在 Google 搜索结果里秒开这个页面),如果你希望参与 V2EX 社区的讨论,你可以继续到 V2EX 上打开本讨论主题的完整版本。

https://www.v2ex.com/t/445791

V2EX 是创意工作者们的社区,是一个分享自己正在做的有趣事物、交流想法,可以遇见新朋友甚至新机会的地方。

V2EX is a community of developers, designers and creative people.

© 2021 V2EX