2017-09-21 2 views
2

아이콘을 클릭하면 아약스 호출을 통해 모델 항목을 삭제하려고합니다. 아약스 호출이 없으면 모든 것이 잘 작동합니다.Laravel 5.5 클릭하면 아약스 호출로 항목 삭제

내 크롬 개발 도구

"심포니 \ 구성 요소 \ HttpKernel \ 예외 \ HttpException"내 네트워크 탭에서 볼 때이 예외가 발생는

이 내 아이콘입니다 :

<i class="fa fa-trash-o deletebtn" aria-hidden="true" data-pointid="<?php echo $damagePoint->id ?>"></i> 

내 아약스 전화 :

$(".deletebtn").click(function(ev){ 
    let pointid = $(this).attr("data-pointid"); 

    $.ajax({ 
     url: '/pointdelete/' + pointid, 
     type: 'delete', 
     success: function (response) { 

     } 
    }); 
}) 

내 경로 :

public function delete($id) 
{ 
    $todo = DamagePoint::findOrFail($id); 
    $todo->delete(); 

    return back(); 
} 
+0

당신은 크롬 개발자 도구에서 요청 헤더의 스크린 샷을 공유 할 수 있습니다.? –

답변

3

당신이 경로를 삭제 사용하는 경우가 post.Here 유사 같다

Route::delete('pointdelete/{id}', '[email protected]'); 

내 컨트롤러 방법은 code.you이 당신의 필요에 따라 변경할 수있는 샘플입니다

$(".deletebtn").click(function(ev){ 
    let pointid = $(this).attr("data-pointid"); 
    $.ajax({ 
       type: 'DELETE', 
       url: '/pointdelete', 
       dataType: 'json', 
       headers: { 'X-CSRF-TOKEN': $('meta[name="csrf-token"]').attr('content') }, 
       data: {id:pointid,"_token": "{{ csrf_token() }}"}, 

       success: function (data) { 
         alert('success');    
       }, 
       error: function (data) { 
        alert(data); 
       } 
    }); 
}); 

경로

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

컨트롤러

public function delete(Request $request){ 

     if(isset($request->id)){ 
       $todo = DamagePoint::findOrFail($request->id); 
       $todo->delete(); 
       return 'success'; 
     } 
} 
+0

그래서 post 메서드를 사용하여 폼을 사용해야합니까? – Chris

+0

아니요 삭제 방법 – iCoders

+0

질문에 따라 @ Chris.updated 대답 – iCoders

0

것은 무엇 URL하는 것은 당신의 크롬 개발자 도구에 요청 헤더 정보에 호출되어 있는지 확인

$(".deletebtn").click(function(ev){ 
    let pointid = $(this).attr("data-pointid"); 

    $.ajax({ 
     url: '/pointdelete/' + pointid, 
     data : {'_method':'delete','_token':'your csrf token'}, 
     //type: 'delete', 
     type:'post', 
     success: function (response) { 

     } 
    }); 
}) 

또한 Ajax 호출의 종류를 사용해보십시오. 아약스에 {{ csrf_token() }}으로 토큰을 넣으려고하면 양식이 필요하지 않습니다.

3

코드 아래에 확인하시기 바랍니다 :

Route::delete('pointdelete',['as' => 'point.delete','uses' => '[email protected]']); 

<button class="fa fa-trash-o deletebtn" aria-hidden="true" onclick="delete({{ $damagePoint->id }}"></button> 

<script type="text/javascript"> 
function delete(id){ 

     var _token = "{{ csrf_token() }}"; 

     $.ajax({ 
      url  : "{{URL::route('your-delete-route')}}", 
      type : 'delete', 
      dataType : 'json', 
      data : { 
          'id': id, 
          '_token':_token 
         }, 
      beforeSend : function() { 

      }, 
      complete : function() { 
      }, 
      success : function(resp) 
      { 
      console.log(resp); 
      } 
     }); 
    } 
</script> 

public function delete(Request $request,$id) 
{ 
DamagePoint::destroy($id); 

return response()->json(['success' => true],200); 
}