[英]Invalid argument error while posting form data using JQuery ajax
I am developing a webapplications in which i have to show search results without submitting the form. 我正在开发一个Web应用程序,其中必须显示搜索结果而不提交表单。 So i have used
$.ajax()
function of jQuery. 因此,我使用了jQuery的
$.ajax()
函数。 Have a look at it 看看它
$('#search').click(function() {
alert("submit handler has fired");
$.ajax({
url: 'cityResults.htm',
data: $('#cityDetails').serialize(),
type: 'json',
success: function(data){
alert(data);
},
error: function(jqXHR, textStatus, errorThrown){
alert('error: ' + textStatus + ': ' + errorThrown);
}
});
return false;//suppress natural form submission
});
Here is my HTML markup. 这是我的HTML标记。 I am using Spring MVC 3 Framework
我正在使用Spring MVC 3 Framework
<form:form id="cityDetails" method="post"
action="cityResults.htm">
<table class="tdbgact border">
<tr>
<th>State: <span class="required">*</span></th>
<td>
<form:select path = "state" id = "state">
<form:option value="">---Select</form:option>
<form:options items="${stateList}"/>
</form:select>
</td>
<th><form:checkbox path="status" value="Active"/>Active</th></td>
<td colspan="5"><th><form:checkbox path="status" value="Inactive"/>Inactive</th></td>
<tr>
<td colspan="2"><input type="button" id="search" class="buttonSubmit" value="City" /></td>
</tr>
</tr>
</table>
This is my Controller Code 这是我的控制器代码
@RequestMapping(value = "/cityResults.htm", method = RequestMethod.POST)
public @ResponseBody String schemeResults(ModelMap model ,@ModelAttribute("SpringWeb")CityMaster bean,
BindingResult result) {
functionName = "cityResults";
List<CityMaster> cities = new ArrayList() ;
logger.info("In "+className+" - "+functionName+" ");
if(result.hasErrors())
{
}
else{
ApplicationContext context =
new ClassPathXmlApplicationContext("Beans.xml");
MasterJDBCTemplate dao =
(MasterJDBCTemplate)context.getBean("masterJDBCTemplate");
String[] cityStatus = bean.getStatus();
if(cityStatus == null)
{
}
cities = dao.cityList(bean);
model.addAttribute("cityList", cities );
}
return cities.get(0).getCity();
}
I get an error in the alert box saying Invalid Argument.Which means my ajax call fails. 我在警告框中收到一个错误消息,说Invalid Argument。这意味着我的Ajax调用失败。 That means i have to change my controllers code.
这意味着我必须更改我的控制器代码。 What changes do i have to do ?
我必须做些什么改变? Or is there anything else i am missing.
还是还有其他我想念的东西。
These links might help you mapping your controller and returning json in response body. 这些链接可以帮助您映射控制器并在响应正文中返回json。
http://www.javacodegeeks.com/2013/07/spring-mvc-requestbody-and-responsebody-demystified.html?ModPagespeed=noscript http://www.javacodegeeks.com/2013/07/spring-mvc-requestbody-and-responsebody-demystified.html?ModPagespeed=noscript
Returning JsonObject using @ResponseBody in SpringMVC 在SpringMVC中使用@ResponseBody返回JsonObject
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.