将 JSON 数据传递给 spring Controller 有多少种方式?
我关注了this tutorial他们使用以下语法传递数据:
data: "{\"name\":\"hmkcode\",\"id\":2}",
这行得通,但由于我需要使用文本输入从用户那里检索数据,所以我不知道如何将我的变量放入该字符串中。
我尝试使用以下语法:
data: "{\"name\":\name\}"
但它返回以下错误:
status: parsererror er:SyntaxError: Unexpected tokken a
我见过其他使用以下语法的网站:
data: {"name":name}
但这给了我同样的错误。
这可行,但我不知道这是否是最佳方法。
var json = {"name" : name};
...
data: JSON.stringify(json),
我设法将 JSON 字符串传递给我的一个 Controller ,但我得到的字符串如下:
{"name": Joe, "lastname": Smith}
有没有办法只在 Person 对象中获取该信息,或者至少只在一个字符串中获取 Joe 而在另一个字符串中获取 Smith?
<script src="http://ajax.googleapis.com/ajax/libs/jquery/2.0.2/jquery.min.js"></script>
<script type="text/javascript">
function doAjaxPost()
{
// get the form values
var name = $('#name').val();
var lastname = $('#lastname').val();
var json = {"name" : name, "lastname" : lastname};
//console.log(json);
$.ajax(
{
type: "POST",
url: "formShow",
data: JSON.stringify(json),
//data: "{\"name\":name}",
//data: {"name":name},
contentType: "application/json; charset=utf-8",
dataType: "json",
cache: false,
beforeSend: function(xhr)
{
xhr.setRequestHeader("Accept", "application/json");
xhr.setRequestHeader("Content-Type", "application/json");
},
success: function(data)
{
//console.log(data);
console.log(data.name);
//var data = $.parseJSON(JSON.stringify(response));
//alert(data);
alert( "name: "+data.name);
//$('#name').val('');
},
error:function(data,status,er) {
alert("error: "+data+" status: "+status+" er:"+er);
}
/* error: function (xhr, ajaxOptions, thrownError)
{
alert(xhr.status);
alert(xhr.responseText);
alert(thrownError);
}*/
});
}
</script>
<fieldset>
<legend>Name in view</legend>
Name in view: <input type="text" id="name" name="name">
<br>
Last Name in view: <input type="text" id="lastname" name="lastname">
<br>
Show modify name in view: <input type="text" id="modifyname" name=""modifyname"">
<br>
<input type="button" value="Add Users" onclick="doAjaxPost()">
</fieldset>
<br>
这些是我的 Controller :
@RequestMapping(value = "formShow", method = RequestMethod.GET)
public String formularioIncidencia (Model model) {
return "formShow";
}
@RequestMapping(value = "formShow", method = RequestMethod.POST)
public @ResponseBody String getTags(@RequestBody String name)
{
String variableAjax= name;
System.out.println("controller variable is " + variableAjax);
//that prints me this "{name: Joe, lastname: Smith}"
return variableAjax;
}
已编辑**** 这是我的用户类
public class Userimplements Serializable {
private static final long serialVersionUID = 1L;
private String name;
private String lastname;
public User(){}
}
我将我的 Controller 编辑为以下内容
@RequestMapping(value = "formShow", method = RequestMethod.GET)
public String formShow(Model model) {
return "formShow";
}
@RequestMapping(value = "formShow", method = RequestMethod.POST)
public @ResponseBody User getTags(@RequestBody final User user, Model model)
{
//what should i do here parse my user to JSON how??
user.setName("name changed");
model.("modifyname", user.getName() );
return User;
}
最佳答案
从 Ajax 中,您还可以将数据作为 data:'name='+ name+'&lastname='+ lastname,
在 Controller 端,您可以使用 @RequestParam
注释来获取从 ajax 调用传递的值。
Ajax 代码如下所示:
$.ajax({
type: 'POST',
url:'your controller url',
data:'name='+ name+'&lastname='+ lastname,
success: function(msg){
alert('wow' + msg);
}
});
Controller 代码:
@RequestMapping(value = "formShow", method = RequestMethod.POST)
public String getTags(@RequestParam("name") String name, RequestParam("lastname") String lastname)
{
System.out.println("name: " + name+ " lastname: "+lastname);
String fullName = name + lastname;
return fullName;
}
希望对您有所帮助。 干杯:)
关于java - 如何在 spring MVC 的 Controller 中操作 jQuery AJAX JSON 数据,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28480258/