【问题标题】:Delete functionality not working for MySQL table?删除功能不适用于 MySQL 表?
【发布时间】:2018-05-11 11:08:25
【问题描述】:

我的删除按钮无法从我的表格中删除条目。任何想法为什么?删除脚本显示在我的网络选项卡上,但是当我单击删除按钮时,它只是将我重定向到页面。

这是我显示页面的获取路径

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

这是我的删除路线

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>

【问题讨论】:

    标签: javascript jquery mysql node.js


    【解决方案1】:

    /pokemon 是您的根网址吗?如果没有,在您的 ajax 调用中,您的 url 是 url: '/pokemon/' + id,但在您的节点路由器中,您期望的是“/id”。应该是

    router.delete('/pokemon/:id', function(req, res) {
       ....
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2015-11-27
      • 2017-04-15
      • 2017-05-13
      • 1970-01-01
      • 2014-11-24
      • 2019-11-15
      • 1970-01-01
      相关资源
      最近更新 更多