doumao6212 2017-10-03 14:42
浏览 76

Laravel上的错误状态500 Ajax

Good I am trying to delete through ajax but I get the following error:

Failed to load resource: the server responded with a status of 500 (Internal Server Error)

I searched the error and apparently appears by the token so I have done what they recommended, I added in the view this:

<meta name="csrf-token" content="{{ csrf_token() }}">

ajax:

$('#delete').on('click', function(){
    var x = $(this);
    var delete_url = x.attr('data-href')+'/'+x.attr('data-id');

    $.ajax({
        url: delete_url,
        type:'DELETE',
        headers:{
            "X-CSRF-TOKEN": $('meta[name="csrf-token"]').attr('content')
        },
        success:function(result){
            alert('success');
        },
        error:function(result){
            alert('error');
        }
    });
});

controller:

public function destroy($id)
{
    $appointment = Appointment::find($id);

    if(appointment == null) {
        return Response()->json([
            'message'=>'error delete.'
        ]);
    }

    $appointment->delete();

    return Response()->json([
        'message'=>'sucess delete.'
    ]);
}

route:

Route::name('appointments.destroy')->delete('/citas/{id}', 'AppointmentController@destroy');

it is certainly a token error because if I do not need it on the route it does it perfectly ...

class VerifyCsrfToken extends BaseVerifier
{
    /**
     * The URIs that should be excluded from CSRF verification.
     *
     * @var array
     */
    protected $except = [
        'citas/*'
    ];
}
  • 写回答

1条回答 默认 最新

  • doutou19761022 2017-10-03 14:46
    关注

    You can check the exact error on developers console, network tab. Look there for your request and check the preview or open it in a new chrome's tab.

    Posted code has a typo, maybe it is in your file too? You missed to add $ to your appointment variable.

    public function destroy($id)
    {
        $appointment = Appointment::find($id);
    
        if($appointment == null) {
            return Response()->json([
                'message'=>'error delete.'
            ]);
        }
    
        $appointment->delete();
    
        return Response()->json([
            'message'=>'sucess delete.'
        ]);
    }
    
    评论

报告相同问题?

悬赏问题

  • ¥15 用windows做服务的同志有吗
  • ¥60 求一个简单的网页(标签-安全|关键词-上传)
  • ¥35 lstm时间序列共享单车预测,loss值优化,参数优化算法
  • ¥15 Python中的request,如何使用ssr节点,通过代理requests网页。本人在泰国,需要用大陆ip才能玩网页游戏,合法合规。
  • ¥100 为什么这个恒流源电路不能恒流?
  • ¥15 有偿求跨组件数据流路径图
  • ¥15 写一个方法checkPerson,入参实体类Person,出参布尔值
  • ¥15 我想咨询一下路面纹理三维点云数据处理的一些问题,上传的坐标文件里是怎么对无序点进行编号的,以及xy坐标在处理的时候是进行整体模型分片处理的吗
  • ¥15 一直显示正在等待HID—ISP
  • ¥15 Python turtle 画图