2017-02-10 6 views
0

私はspringsとajaxを初めて使用しています 私は動的に作成されたJavaスクリプトにjsonオブジェクトを持っています 私はこのjsonオブジェクトをJavaスクリプトからajaxまたは通常のsubmit )JavaスクリプトからjavaにJavaを渡す

文字列の場合、非表示の入力があります。私は JSONオブジェクト間違っていないよ場合

私の知識のよう私たちは隠された

に格納することができないと私は、Javaコードを使用して受信することがあります。 これは私のスクリプト

$(document).ready(function(){ 
    // click on button submit 
    $("#save_btn").on('click', function(){ 
     alert(); 
     // send ajax 
     $.ajax({ 
      url: 'project_reg_save', // url where to submit the request 
      type : "POST", // type of action POST || GET 
      dataType : 'json', // data type 
      data : $("#reg_form").serialize(), // post data || get data 

      success : function(result) { 
       // you can see the result from the console 
       // tab of the developer tools 
       console.log(result); 
      }, 
      error: function(xhr, resp, text) { 
       console.log(xhr, resp, text); 
      } 
     }) 
    }); 
}); 

であり、これは

@Controller 
public class Save { 
    @RequestMapping("/project_reg_save") 

    public ModelAndView mymethod(@RequestParam JSONObject obj)//which is not possible { 

     System.out.println(obj); 

     return new ModelAndView("Product_reg", "msg", "product Registration"); 
    } 
} 
+0

多分それをシリアル化するためにJavaを使用し、その後、それを文字列として渡してみては? –

+0

リクエストパラメータではなく、リクエスト本体です。アノテーションを '@ RequestBody'に変更し、' @ RequestMapping'に 'consumes = {MediaType.APPLICATION_JSON_VALUE}'を追加してみてください。 –

答えて

0

一つの解決策は

  1. 変化にあなたのデータ型です私のJavaコードです:データ型に 'JSON': 'テキスト'
  2. @RequestParam JSONObject obj to @RequestParam String obj
  3. JsonObject JsonObj =新しいJsonParser()。parse(obj).getAsJsonObject();

ので、あなたはJSONオブジェクトを取得

関連する問題