0

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()

...

Alberto Crespo
  • 2,429
  • 5
  • 28
  • 51

1 Answers1

1

You either get a response or an error. If you look in the error console, you will most likely find an error message there that says that you are trying to use the err variable which is undefined.

Correct the error handler, and you will see what error you get:

    error: function (xhr, status, error) {
        alert(status);
        alert('Cannot update, ' + error);
    }
Guffa
  • 687,336
  • 108
  • 737
  • 1,005
  • Ok thank you!, the error response is `Cannot update, Forbidden`. Any idea? – Alberto Crespo Nov 20 '14 at 20:46
  • @AlbertoCrespo: That means that the server responds with a `HTTP 403 Forbidden`. A likely reason for that would be that the mapping doesn't match the request, so there is no action for handling the request. – Guffa Nov 20 '14 at 20:55
  • the URL seems to be mapped. I have updated the Question. You can see that the URL appears as mapped in the MVC Spring Server. – Alberto Crespo Nov 21 '14 at 12:27
  • @AlbertoCrespo: I think that's because the JSON that you send doesn't match the data type of the action parameter. The JSON will be deserialised into an array of strings, but the action parameter is a string. The answers here should be helpful: http://stackoverflow.com/questions/16467035/spring-mvc-dont-deserialize-json-request-body – Guffa Nov 21 '14 at 13:11