简体   繁体   中英

Laravel: $request->all() gives empty array when called with json xhr

For some reason I cannot use $.ajax, only the XMLHttpRequest. I need to send json to laravel controller. When I tried it, I only got 500s

Here's how I make the request:

    const sendEdit = function(){
        let xhr = new XMLHttpRequest();
        xhr.open("POST", "/blog/edit");
        xhr.setRequestHeader("Content-Type","application/json");
        xhr.setRequestHeader('X-CSRF-TOKEN',   $('meta[name="csrf-token"]').attr('content'))
        let data = {};
        data.header = $("#editHeader").val();
        data.body = $("#editBody").val();
        data.postId = {{$post->id}};
        data.userId = {{Auth::user()->id}}
        xhr.onreadystatechange = function(d){
        }
        xhr.send([data]);
    }

The controller returned 500. When I tried to var_dump $request->json() or $request->all() it showed me an error. Here's my controller. Please help me access the data in JSON

 public function edit(Request $request){
         echo(var_dump($request->all()));
    }

问题是在发送请求之前未使用JSON.stringify()。

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM