2017-01-08 7 views
1

私は基本的に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(内部サーバーエラー)を削除

+0

は'タイプを使用してみてください –

答えて

1

Ajaxの呼び出しは非同期であるとして、あなたは最初の.doneコールバック内からitemDelete(ID)を呼び出す必要があります。ただ

var id = newResult[0]._id; 

あなたが最初に終了した後にのみ2番目のAJAX呼び出しを実行します。この方法の後

itemDelete(id); 

を移動してみてください 、およびID変数が定義されます。あなたのコードは次のようになります。変更後

$(".delete-item").on('click', function(e, id) { 
    var deleteName = $('p.nameLink').text(); 

    $.ajax({ 
     type: "GET", 
     url: "/items", 
     dataType: 'json', 
    }) 
    .done(function(result) { 
     var newResult = $.grep(result, function(e){ 
      return e.name === deleteName; 
     }); 

     var id = newResult[0]._id; 
     itemDelete(id); 
    }) 
    .fail(function (jqXHR, error, errorThrown) { 
     console.log(jqXHR); 
     console.log(error); 
     console.log(errorThrown); 
    }); 

    function itemDelete(id) { 
     $.ajax({ 
      type: 'DELETE', 
      url: '/items/' + id, 
       dataType: 'json', 
      }) 
      .done(function(result) { 


      }) 
      .fail(function (jqXHR, error, errorThrown) { 
       console.log(jqXHR); 
       console.log(error); 
       console.log(errorThrown); 
     }); 
    } 
}); 
0

undefinedhttps://www.someplace.com/items/undefinedでは、idが未定義であることを示唆しています。取得要求と削除要求は非同期で発生するため、削除要求の作成時にidが設定されていることが保証されていないことに注意してください。つまり、の後にを削除しようとすると、そのIDを取得します。これには、GET要求の成功コールバックでDELETE要求を行う必要があります。

1

これはあなたのために働くはずです。

$( "項目を削除します。")( 'クリック'、機能(E、ID){ VAR deleteName = $( 'p.nameLink')上のテキスト();。。

// ID最初

  $.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; 
      itemDelete(id); // pass the id to the function 
     }) 
     .fail(function (jqXHR, error, errorThrown) { 
      console.log(jqXHR); 
      console.log(error); 
      console.log(errorThrown); 
     }); 



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

     } 


    }); 

おかげ

0

を取得し、あなたのURLページやアクションでこの1

$.ajax({ 
      type: 'get', 
      dataType : 'html', 
      url: 'your_url', 
      data:{variablname:id,type:'DELETE'} 
      dataType: 'json', 
     }) 

は値を取得してみてください「POST'`ではなく `タイプ:DELETE''`そのタイプのため、データ型が、私はそれはあなたを助けることを願っています

if(isset($_GET['variablename'],$_GET['type'])){ 
    if($_GET['type']=='DELETE') 
     delete record from database where id= $_GET['variablename'] 
} 

をGETなりますされるようおかげ

関連する問題