2017-04-20 75 views
0

使用JSON條目我已動態生成的表也有這個按鈕:刪除在Laravel

<button class="btn btn-danger btn-xs btn-delete delete-task" 
value="{{$contact->id}}">delete</button> 

在代碼結束時,我有這樣的:

<meta name="_token" content="{!! csrf_token() !!}" /> 

按鈕觸發該:

$(document).ready(function(){ 

$('.delete-task').click(function(){ 
    var contact_id = $(this).val(); 

    $.ajax({ 
     type: "DELETE", 
     url: adressbook_edit + '/' + contact_id, 
     success: function (data) { 
      console.log(data); 
      $("#contact" + contact_id).remove(); 
     }, 
     error: function (data) { 
      console.log('Error:', data); 
     } 
    }); 
}); 
} 

這應該引起我的路線是這樣的:

Route::delete('/adressbook_edit/{$contact_id?}',function($contact_id){ 
    $contact = addressbook::destroy($contact_id); 
    return Response::json($contact); 
}); 

我期待刪除數據庫中的條目,但是我得到一個404錯誤。方向顯然是正確的。這是我得到的錯誤:

DELETE http://myip/adressbook_edit/2 404 (Not Found) send @ app.js:26 ajax @ app.js:25 (anonymous) @ adressbook.js:79 dispatch @ app.js:25 g.handle @ app.js:25 adressbook.js:87

Error: Object {readyState: 4, getResponseHeader: function, getAllResponseHeaders: function, setRequestHeader: function, overrideMimeType: function…}

Adressbook.js是前面提到的ajax函數被調用的地方。

+2

那是正確的'刪除( '/ adressbook_edit/{$ CONTACT_ID?}' ''我認爲不需要'$'和'?'。試試用'delete('/addressbook_edit/ {contact_id}' –

+0

@AntonisTsimourtos嘗試沒有區別 – prgrm

+0

除了What @AntonisTsimourtos建議你在ajax中的url屬性請求需要如下所示:'url:'adressbook_edit /'+ contact_ id'或'url:'{{url(「adressbook_edit」)}}'+'/'+ contact_id' –

回答

1

試圖將一個AJAX網址改成這樣:

url: '/adressbook_edit/' + contact_id 

,改變你的路線,這一點:

Route::delete('/adressbook_edit/{contact_id}',function($contact_id){ 
    $contact = addressbook::destroy($contact_id); 
    return Response::json($contact); 
});