2017-11-28 22 views
0

テーブルからエントリを削除するには、削除ボタンが機能していません。なぜどんなアイデア?削除スクリプトはネットワークタブに表示されていますが、削除ボタンをクリックすると、ページにリダイレクトされます。ここでMySQLテーブルで削除機能が動作しませんか?

はここ

router.get('/', function(req, res){ 
    var callbackCount = 0; 
    var context = {}; 
    context.jsscripts = ["deletepokemon.js"]; 
    var mysql = req.app.get('mysql'); 
    getPokemon(res, mysql, context, complete); 
    getMoves(res, mysql, context, complete); 
    function complete() { 
     callbackCount++; 
     if(callbackCount >= 2){ 
      res.render('pokemon', context); 
     } 
    } 
}); 

ページを表示するために私のgetルートである私の削除ルート

router.delete('/:id', function(req, res) { 
    var mysql = req.app.get('mysql'); 
    var sql = "DELETE FROM pokemon WHERE id=?"; 
    var inserts = [req.params.id]; 
    sql = mysql.pool.query(sql, inserts, function(error, results, fields){ 
      if(error){ 
      res.write(JSON.stringify(error)); 
      res.status(400); 
      res.end(); 
      } else{ 
        res.status(202).end(); 
      } 
     }) 
    }) 

はここでjQueryの

function deletePokemon(id){ 
     $.ajax({ 
       url: '/pokemon/' + id, 
       type: 'DELETE', 
       success: function(result){ 
        window.location.reload(true); 
     } 
    }) 
}; 

と私の削除スクリプトは、ここに私のハンドルバーですされますファイルをすべてレンダリングするファイル

<table id="table"> 
    <thead> 
     <th>Pokemon Name </th> 
     <th>Evolution Level </th> 
     <th>Move Name </th> 
    </thead> 
    <input type="text" class="search form-control" id="searchinput" placeholder="Pokemon Name"> 
    <input type="button" class="btn btn-primary" value="Search" onclick="getUsers({{searchinput}})"> 
     <br> 
    <tbody> 
     {{#each pokemon}} 
     <tr> 
      <td>{{pokemonname}}</td> 
      <td>{{evolutionlevel}}</td> 
      <td>{{movename}}</td> 
      <td><button onclick="deletePokemon({{id}})">Delete</button></td> 
     <td><a href="/pokemon/{{id}}">Update</a></td> 
     </tr> 
     {{/each}} 
    </tbody> 
    <form id="addpokemon" action="/pokemon" method="POST"> 
     Pokemon Name: <input type="text" name="pokemonname"><br> 
     Evolution Level: <input type="text" name="evolutionlevel"><br> 
     Move Name: <select name="movename"> 
     {{#each move}} 
     <option value="{{id}}">{{primarymovename}}</option> 
     {{/each}} 
     </select><br> 
     <input type="submit" value="Submit"> 
    </form> 
</table> 

答えて

0

/pokemonはルートURLですか?そうでなければ、あなたのajax呼び出しでは、あなたのURLはurl: '/pokemon/' + idですが、あなたのノードルーターでは '/ id'が必要です。それは、

です。
関連する問題