2016-03-21 6 views
0

Ich habe Ajax Json POST-Methode wie folgt.Ajax Json POST und Spring MVC Controller 8

$(function() { 
       $('#formId').submit(function (event) { 
        event.preventDefault(); // prevent this form from being submited 
        var userJson = $('#id').val(); 
        alert(userJson); 
        $.ajax({ 
         type: "POST", 
         url: "/MobitelProgressTool/ajaxcall", 
         data: userJson, 
         contentType: "application/json; charset=utf-8", 
         dataType: "json", 
         success: function (data, textStatus, jqXHR) { 
          alert(data);//handle it in a proper way 
         }, 
         failure: function (jqXHR, textStatus, errorThrown) { 
          alert(textStatus);//handle it in a proper way 
         } 
        }); 
        alert("mm"); 
        return false; 
       }); 
      }); 

Controller-Post-Anforderung

@RequestMapping(value = {"/ajaxcall"}, method = RequestMethod.POST) 
    @ResponseBody// <== this annotation will bind Arr class and convert to json response. 
    public List<String> addAnotherAppointmenttt(HttpServletRequest request, HttpServletResponse response, @RequestBody String userJson, Model model, BindingResult errors) { 
     System.out.println("*******88888" + userJson); 
     //List<String> ll = stageViiChartDataServices.findByUpdated_Scope(userJson); 
     List<String> messages = Arrays.asList("Hello", "World!", "How", "Are", "You"); 
     return messages; 
    } 

aber ich kann nicht immer mesages Listenwerte zu behandeln ,. Ich bin mir nicht sicher, wie ich obigen Code korrigieren soll.

Antwort

0

Lesen Sie Ihr Javascript Ich sehe, dass Sie einen Wert in diesem Formular var userJson = $('#id').val();.

Auf diese Weise fragen Sie bei jQuery nach einem Wert. Sie sollten jedoch einen JSON und keinen Wert veröffentlichen. Ein Think wie dies kann in Ordnung sein:

var value = $('#id').val(); 
var json = {'id': value} 

dann meine persönliche Beratung war nicht zu verwenden, wenn es möglich HttpServletRequest request, HttpServletResponse response in Ihrem Controller ist, statt Frühling Abstraktion verwendet, @RequestBody String userJson, Model model, BindingResult errors ist in Ordnung!

Ich hoffe, dass dies Ihnen helfen kann