I have an ajax call to a spring controller, then write the returned data to DOM.
Direct access controller in browser works fine and displays a string.
Run the ajax in debugger showing that ajax call fired and controller got called, then ajax call always failed. Any suggestion? Here’s my code.
servlet-context.xml:
<context:component-scan base-package="com.test.web.controller" />
controller method:
@RequestMapping(value = "/myAjaxHandler")
public void myAjaxHandler(HttpServletRequest request, HttpServletResponse response){
response.setContentType("text/html");
response.getWriter().println("Hello");
}
ajax code:
$.ajax({
type: 'POST',
url: '/dispatcher/myAjaxHandler',
data: $("form").serialize(),
dataType: "text",
success: function(data) {
alert("success");
},
error: function(){
alert("fail");
}
});
try using
myAjaxCall()insted ofvar myAjaxCall = function() = ...