如何在Laravel上使用普通Javascript获取发布数据?

如何在Laravel上使用普通Javascript获取发布数据?,javascript,ajax,laravel,fetch,Javascript,Ajax,Laravel,Fetch,我在javascript上使用此方法将数据发布到数据库中: if(document.getElementById('approve_leave_request')){ document.getElementById('approve_leave_request').addEventListener('click', ()=>{ const urlReq = window.location.origin+"/leave/customize"; cons

我在javascript上使用此方法将数据发布到数据库中:

if(document.getElementById('approve_leave_request')){
    document.getElementById('approve_leave_request').addEventListener('click', ()=>{
        const urlReq = window.location.origin+"/leave/customize";
        const token = document.querySelector('meta[name="csrf-token"]').getAttribute('content');

        const leave = {
            status: 'Approved',
        };

        const request = new Request(urlReq, {
            method: 'POST',
            body: JSON.stringify(leave),
            headers: new Headers({
                'Content-Type': 'application/json',
                "X-CSRF-TOKEN": token
            })
        });

        fetch(request).then(res => res.json()).then(res => console.log(res));

    })
}
这是我的控制器功能:

public function customize_update(Request $request){
    return response()->json(['message'=>'Called successfully.']);
}
在我的api上:

Route::post('leave/customize', 'LeaveController@customize_update')->name('api.leave.customize');
但在执行过程中,我遇到了以下错误:

POST http://127.0.0.1:8000/leave/customize 405 (Method Not Allowed)
Uncaught (in promise) SyntaxError: Unexpected token < in JSON at position 0
POSThttp://127.0.0.1:8000/leave/customize 405(方法不允许)
未捕获(承诺中)语法错误:JSON中位置0处的意外标记<

您的路线不应该是
http://127.0.0.1:8000/api/leave/customize
?对不起,我弄错了是的,这是
http://127.0.0.1:8000/api/leave/customize