dsfdsf8888 2017-04-20 12:46
浏览 150
已采纳

在Laravel中删除带有JSON的条目

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.

  • 写回答

1条回答 默认 最新

  • duanji5116 2017-04-20 14:04
    关注

    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);
    });
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 乌班图ip地址配置及远程SSH
  • ¥15 怎么让点阵屏显示静态爱心,用keiluVision5写出让点阵屏显示静态爱心的代码,越快越好
  • ¥15 PSPICE制作一个加法器
  • ¥15 javaweb项目无法正常跳转
  • ¥15 VMBox虚拟机无法访问
  • ¥15 skd显示找不到头文件
  • ¥15 机器视觉中图片中长度与真实长度的关系
  • ¥15 fastreport table 怎么只让每页的最下面和最顶部有横线
  • ¥15 R语言卸载之后无法重装,显示电脑存在下载某些较大二进制文件行为,怎么办
  • ¥15 java 的protected权限 ,问题在注释里