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

报告相同问题?

悬赏问题

  • ¥100 嵌入式系统基于PIC16F882和热敏电阻的数字温度计
  • ¥20 BAPI_PR_CHANGE how to add account assignment information for service line
  • ¥500 火焰左右视图、视差(基于双目相机)
  • ¥100 set_link_state
  • ¥15 虚幻5 UE美术毛发渲染
  • ¥15 CVRP 图论 物流运输优化
  • ¥15 Tableau online 嵌入ppt失败
  • ¥100 支付宝网页转账系统不识别账号
  • ¥15 基于单片机的靶位控制系统
  • ¥15 真我手机蓝牙传输进度消息被关闭了,怎么打开?(关键词-消息通知)