在Laravel中删除带有JSON的条目(Deleting an entry with JSON in 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错误。 方向显然是正确的。 这是我得到的错误:

删除http:// myip / adressbook_edit / 2 404(未找到)发送@ app.js:26 ajax @ app.js:25(匿名)@adressbook.js:79 dispatch @ app.js:25 g.handle @ app .js:25 adressbook.js:87

错误:对象{readyState:4,getResponseHeader:function,getAllResponseHeaders:function,setRequestHeader:function,overrideMimeType:function ...}

Adressbook.js是调用前面提到的ajax函数的地方。

I have a dynamically generated table that also have this button:

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

At the end of the code I have this:

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

The button triggers this:

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

Which is supposed to lead to my routes like this:

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

I am expecting to delete the entry in the database, however I get a 404 error. The direction is apparently correct. Here is the error I get:

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 is where the aforementioned ajax function is called.

最满意答案

尝试将您的ajax网址更改为:

url: '/adressbook_edit/' + contact_id

并改变你的路线:

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

Try to change your ajax url to this:

url: '/adressbook_edit/' + contact_id

And also change your route to this:

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

更多推荐