2016-03-21 15 views
0

私はこのようなajax json POSTメソッドを持っています。Ajax json POSTとSpring MVCコントローラ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; 
       }); 
      }); 

コントローラーポスト要求

@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; 
    } 

を処理するが、私はmesagesリストの値を取得することはできません、。上記のコードを修正する方法がわかりません。

答えて

0

あなたのjavascriptを読むあなたはこの書式で値を投稿することがわかりますvar userJson = $('#id').val();

このようにして、jQueryで値を取得するように依頼します。しかし、値ではなく、jsonを投稿するべきです。このように考える細かいことがあります

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

その後、私の個人的なアドバイスは、それはあなたのコントローラでHttpServletRequest request, HttpServletResponse response可能な場合@RequestBody String userJson, Model model, BindingResult errorsは結構です、代わりに春の抽象化を使用して、使用していませんでした!

私はこれがあなたを助けることを願っています

関連する問題