簡體   English   中英

試圖在Ajax中刪除MongoDB文檔不斷出現錯誤404'CANNOT DELETE'

[英]Trying To Delete MongoDB Document In Ajax Keep Getting Error 404 'CANNOT DELETE'

因此,當我單擊使用“ gameName”值的按鈕時,我希望能夠刪除MongoDB中的特定項目。 當我這樣做時,我不斷收到錯誤404“無法刪除”。 我在這里做錯了什么? 謝謝。

這是請求:

function deleteGame() {
$('#js-games').on('click', '.delete', function(event) {
    event.preventDefault();
    console.log('delete button works')
    var id = $(this).parent().data('id');
    console.log(id);
$.ajax({
    type: 'DELETE',
    dataType: 'json',
    contentType: 'application/json; charset=utf-8',
    url: '/favorites' + '/' + id,
    success: getYourGames(),
})
    var gone = 'Your time was deleted!';
    $('.js-type-game').html(gone);
    setTimeout(function() {
        $('.js-type-game').fadeOut(gone).html('').fadeIn();
        }, 2000);
})

}

這是router.delete:

router.delete('/gameName', (req, res) => {
favorite.delete(req.params.gameName);
console.log(`Deleting game \`${req.params.gameName}\``);
res.status(204).end();
});

您不小心定義了一個明確的/gameName路由。 要定義參數,您需要使用冒號,即:

router.delete('/:gameName', (req, res) => { // with colon (:)
    favorite.delete(req.params.gameName); // req.params.gameName is now correct
    console.log(`Deleting game \`${req.params.gameName}\``);
    res.status(204).end();
});

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM