2017-10-12 1 views
0

Ajaxはコントローラにデータを送信します。メソッドはボタンをクリックするとそのデータを使用します。私のmetodにはmodel.addatribute.Alert post null値があります。なぜですか?jqueryでmodel.addAttributeが機能しないのですか?

マイコントローラー

@RequestMapping(value = "index",method =RequestMethod.POST) 

@ResponseBody 公共リストフィールド(@RequestParam( "TNAME")文字列名、RequestParam @( "DNAME")文字列dnameの、モデルモデル){ 文字列のTN =名; 文字列dn = dname; リストconame = new ArrayList <>(); { 接続mycon2 = DriverManager.getConnection( "jdbc:mysql:// localhost:3306"、 "root"、 "1996"); DatabaseMetaData metaData2 = mycon2.getMetaData();ResultSet res4 = metaData2.getColumns(dn、null、tn、 "%"); coname = new ArrayList <>(); while(res4.next()){ 文字列column_name = res4.getString( "COLUMN_NAME"); coname.add(column_name); }

 } 
     catch (Exception e) { 
      // TODO: handle exception 
      e.printStackTrace(); 
     } 
return coname; 
    } 

インデックスHTML

<div id="RightPanel" style="width: 200; height: 100%; float: right;"> 
      <table class="table table-striped"> 
       <input type='text' id='tablehead' /> 
       <tr> 
        <th>Column name</th> 
       </tr> 
       <tr th:each="name : ${cname}"> 
        <td> th:text="${name}"</td> 
       </tr> 
      </table> 
     </div> 

インデックスHTML内のJavaScript

<script th:inline="javascript"> 

    function onclk(a,b) { 
     var search = { 
      "tName" : a, 
      "dName" :b 
     } 
     $.ajax({ 
     type: "POST", 
     url: "http://localhost:8092/index", 
     data: search, 
     dataType: "json", 
     success: function (data) { 
      var modelAttributeValue = [[${coname}]]; 
      alert(modelAttributeValue); }, 
     error: function(){ 
      alert('got error'); 
     } 
    }); 
+0

をマークアップの私の知る限り、このような 'は、メインページのloa ds。これはajax呼び出しのコンテキストでは実行されません。あなたのAjaxコールは、あなたのアクションメソッドを見ることができますが、単に "return"インデックス ";'の結果として1つの文字列 "index"を返します。これは 'success:function(){}'コールバックで 'success:function(data){alert(data);}として再定義すると受け取られます。} 'ajax呼び出しが完了すると、アラートは「インデックス」を表示します(失敗しないと仮定して)。私は、あなたがAjaxの仕組みについての基本的な理解を高める必要があると思います。 – ADyson

答えて

0

これを試してみてください:

<script type="text/javascript" th:inline="javascript"> 
    /*<![CDATA[*/ 
    function onclk(a,b) { 
     var search = { 
      "tName" : a, 
      "dName" :b 
     } 
     $.ajax({ 
     type: "POST", 
     url: "http://localhost:8092/index", 
     data: search, 
     dataType: "json", 
     success: function (data) { 
      var modelAttributeValue = [[${coname}]]; 
      alert(modelAttributeValue); }, 
     error: function(){ 
      alert('got error'); 
     } 
    }); 
    /*]]>*/ 
</script> 
関連する問題