我有像这样的ajax json POST方法。
$(function () {
$('#formId').submit(function (event) {
event.preventDefault(); // prevent this form from being submited
var userJson = $('#id').val();
alert(userJson);
$.ajax({
type: "POST",
url: "/MobitelProgressTool/ajaxcall",
data: userJson,
contentType: "application/json; charset=utf-8",
dataType: "json",
success: function (data, textStatus, jqXHR) {
alert(data);//handle it in a proper way
},
failure: function (jqXHR, textStatus, errorThrown) {
alert(textStatus);//handle it in a proper way
}
});
alert("mm");
return false;
});
});
处理发布请求的控制器
@RequestMapping(value = {"/ajaxcall"}, method = RequestMethod.POST)
@ResponseBody// <== this annotation will bind Arr class and convert to json response.
public List<String> addAnotherAppointmenttt(HttpServletRequest request, HttpServletResponse response, @RequestBody String userJson, Model model, BindingResult errors) {
System.out.println("*******88888" + userJson);
//List<String> ll = stageViiChartDataServices.findByUpdated_Scope(userJson);
List<String> messages = Arrays.asList("Hello", "World!", "How", "Are", "You");
return messages;
}
但是我无法获得消息列表值。我不确定如何纠正上述代码。
答案 0 :(得分:0)
阅读您的javascript我看到您以此格式@Scripts.Render("~/bundles/jqueryui")
<h2>jQuery AutoComplete</h2>
<script>
$(function () {
$('#tags').autocomplete({
source: function (request, response) {
$.ajax({
url: '@Url.Action("ItemAutocomplete")',
extraParams: { term: $('#tags').val(),
dataType: "json",
contentType: 'application/json, charset=utf-8',
data: {
term: $("#tags").val()
},
success: function (data) {
response($.map(data, function (item) {
return {
label: item
};
}));
},
error: function (xhr, status, error) {
alert(error);
}
});
},
minLength: 2
});
});
</script>
<div class="ui-widget">
<label for="tags">Tags: </label>
<input id="tags" />
</div>
发布了一个值。
通过这种方式,您可以在jQuery中查询值。但是你应该张贴一个json,而不是一个值。像这样的想法可能没问题:
var userJson = $('#id').val();
然后我的个人建议是不要使用,如果你的控制器中可能var value = $('#id').val();
var json = {'id': value}
,而是使用Spring抽象,HttpServletRequest request, HttpServletResponse response
没问题!
我希望这可以帮到你