Screen Shot of an ERROR 我试图通过Spring MVC控制器使用AJAX调用来插入值。 但它点击按钮就抛出了对象Erorr。任何人都可以帮我解决这个问题。
CODE: Ajax代码:
<script type="text/javascript">
// $(document).ready(function() {
function doAjaxPost() {
// // get the form values
$.ajax({
type : "POST",
async: "false",
// url : "${pageContext.request.contextPath}/leadstatus_creation",
url :'/ajax/leadstatus_creation',
data: $('#frm-createlead-status').serialize(),
success : function(data) {
if(data != null && data !='')
{
$('#txtleadname').val(data);
}
},
error : function(XMLHttpRequest, textStatus, errorThrown) {
alert(textStatus);
}
});
}
// });
</script>
LeadController.java
/**
* Method use for View lead status get method.
* @param map
* @return
* @throws Exception
*/
@RequestMapping(value="/ajax/leadstatus_creation",method=RequestMethod.POST)
public @ResponseBody String createleadstatus(BindingResult result,HttpSession session,HttpServletRequest request,HttpServletResponse response) throws Exception{
String resultStr=leadDao.createLeadStatus(null);
try
{
if(session !=null)
{
String leadstatus = request.getParameter("txtleadname");
Map<Integer,Object>obj=null;
obj=new HashMap<Integer , Object>();
obj.put(1, leadstatus);
obj.put(2, 1);
if(leadDao.createLeadStatus(obj) != null)
{
resultStr = "true";
}
else
{
resultStr = "false";
}
resultStr = JSONValue.toJSONString(resultStr);
}
response.setContentType("application/json");
response.getWriter().write(resultStr.toString());
} catch (Exception ex) {
System.out.println(ex.getMessage());
}
return resultStr;
}
}
表格代码:
答案 0 :(得分:0)
将id添加到用于在spring controller中发布数据的按钮
<a id="frm-createlead-status" > POST </a>
然后您可以使用'click'事件在页面加载时执行该功能
<script type="text/javascript">
$(document).ready(function(){
$('#frm-createlead-status').on('click', function(event){
$.ajax({
url :'/ajax/leadstatus_creation',
type: 'POST',
data: $(#frm-createlead-status).serialize(),
success: function(data){
if(data != null && data !='')
{
$('#txtleadname').val(data);
}
},
error : function(XMLHttpRequest, textStatus, errorThrown) {
alert(textStatus);
}
});
});
});
</script>