私は基本的にmongodbの項目を削除しようとしています。しかし、私はちょうどajax呼び出しのURLにIDを渡すように見えることはできません。ここに私のコードです:javascriptのajax urlに変数を渡せません
$(".delete-item").on('click', function(e, id) {
var deleteName = $('p.nameLink').text();
// Get ID first
$.ajax({
type: "GET",
url: "/items",
dataType: 'json',
})
.done(function(result) {
// searches mongodb for the clicked name and saves the result in the var
var newResult = $.grep(result, function(e){ return e.name === deleteName; });
var id = newResult[0]._id;
})
.fail(function (jqXHR, error, errorThrown) {
console.log(jqXHR);
console.log(error);
console.log(errorThrown);
});
// Then delete
console.log("ID to DELETE:", id); // I can see id
function itemDelete(id) {
$.ajax({
type: 'DELETE',
url: '/items/' + id,
dataType: 'json',
})
.done(function(result) {
console.log("ID to DELETE:", id); // Can't see the id
})
.fail(function (jqXHR, error, errorThrown) {
console.log(jqXHR);
console.log(error);
console.log(errorThrown);
});
}
itemDelete(id); // pass the id to the function
});
私は現時点で学んでいるので、私はこのことについて間違った道を進んですることができます。誰かが私を助けることができれば、私はそれを感謝します。
エラーメッセージは次のとおりです。
はhttps://www.someplace.com/items/undefined 500(内部サーバーエラー)を削除
は'タイプを使用してみてください –