2017-09-05 92 views
0

使用以下ajax調用來檢索控制器中的參數,但它會給出錯誤。當我在AJAX和控制器中添加參數id時發生。錯誤:內部服務器錯誤Laravel 5.4 ajax調用

$.ajax({ 
    url:'/delprofile', 
    type: 'delete', 
    data: { 
     id: 5, 
     '_token': $('meta[name="csrf-token"]').attr('content') 
    }, 
    success: function(){ alert("Record deleted.") }, 
    error: function(jqXHR, textStatus, errorThrown) { 
     console.log(textStatus + ' : ' + errorThrown); 
    } 
}); 

Laravel功能:

Route::delete('/delprofile','[email protected]'); 

public function delprofile (Request $request){ 
    $id=$request->input('id'); 
    DB::table('education')->where('id','=',id)->delete(); 
} 
+0

我會建議它可能是在功能的laravel部分問題。也許檢查'$ id'是否實際上是先填充的? – Deckerz

+0

@Deckerz如何檢查,因爲我同時從ajax傳遞靜態值。 –

+0

嘗試將其輸出到PHP中的錯誤日誌中,只需看看是否實際記錄了5。如果工作正常,則錯誤必須在數據庫查詢中。 – Deckerz

回答

0

ID是可變的,而不是$idid

此外,對於更好的代碼:

1 - 你在你的AJAX調用指定的方法作爲標準"POST"

2 - 您添加到您的數據:_method: 'delete'因爲這是推薦的方法來發送DELETEPATCHPUT請求Laravel。

3-添加一個return response()->json($id);以確保ID已成功發送。

在你的成功的功能4-,CONSOLE.LOG的ID success: function(data){console.log(data); alert("Record deleted.")....

+0

同樣的錯誤顯示 –

+0

什麼是錯誤? – Sletheren

+0

錯誤:內部服務器錯誤 –