I am trying POST a String array from jQuery to MVC Spring Server.
POST code:
var respostas=["hello","hi","bye"]; var urlprova = 'getlistmap'; $.ajax({ type: 'POST', url: urlprova, dataType: 'json', data: JSON.stringify(respostas), success: function (data) { alert("updated successfully"); }, error: function (xhr, ajaxOptions, error) { alert(err.status); alert('Cannot update, ' + err.responseText); }
});
Controller method:
@RequestMapping(value="getlistmap", method = RequestMethod.POST) public @ResponseBody String getListMapfromOneProject(HttpServletResponse response, Principal p,@RequestBody String respostas) throws IOException { System.out.println("A AJAX call has been detected!"); return "ok!"; }
I have tried a lot of piece of code from google but any of them seems working, so I typed above that I think is the best option.
Any ideia?
UPDATE: After the correction that @Guffa gives to me, the response of this POST is, HTTP 403 Forbidden
he said that the problem could be that the server didn't map the url, but we can comprove that this is mapped:
2014-11-20 18:44:53.427 INFO 6724 --- [ main] o.s.w.s.handler.SimpleUrlHandlerMapping : Mapped URL path [/] onto handler of type [class org.springframework.web.servlet.resource.DefaultServletHttpRequestHandler] 2014-11-20 18:44:53.466 INFO 6724 --- [ main] s.w.s.m.m.a.RequestMappingHandlerMapping : Mapped "{[/login],methods=[GET],params=[],headers=[],consumes=[],produces=[],custom=[]}" onto public org.springframework.web.servlet.ModelAndView palmaslab.mapas.controller.myController.login() 2014-11-20 18:44:53.466 INFO 6724 --- [ main] s.w.s.m.m.a.RequestMappingHandlerMapping : Mapped "{[/getlistmap],methods=[POST],params=[],headers=[],consumes=[],produces=[],custom=[]}" onto public java.lang.String palmaslab.mapas.controller.myController.getListMapfromOneProject(javax.servlet.http.HttpServletResponse,java.security.Principal,java.lang.String) throws java.io.IOException 2014-11-20 18:44:53.467 INFO 6724 --- [ main] s.w.s.m.m.a.RequestMappingHandlerMapping : Mapped "{[/addProjectPostoSaude],methods=[GET],params=[],headers=[],consumes=[],produces=[],custom=[]}" onto public org.springframework.web.servlet.ModelAndView palmaslab.mapas.controller.myController.addProject()
...