Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

binggle's avatar

how to get Post data from js fetch formData ?

my laravel code want to get data from frontend fetch api.

// javascript
var data = new FormData()

data.append('count', count )
data.append('timestamp', timestamp)

console.log(data);

fetch('/time', {
        method: 'POST',
        headers: {
            'Content-Type': 'application/json',
            'X-CSRF-TOKEN': csrf_token
        },
        body: data,
    })
    .then((response) => {
        return response.json();
    })
    .then((data) => {
        
    })
    .catch((error) => {
        console.error('Error:', error);
    });

// laravel Controller 
function time( Request $request ){
    dd($request->all());
}

But dd returns []

how can I get data from fetch FormData ?

0 likes
6 replies
vincent15000's avatar

Show your /time route please.

Why using fetch ? What is your front environment ?

binggle's avatar

@vincent15000

Hi . thanks for reply.

// routes/web.php

Route::post('/time',[ UploadController::class, 'time'] );

I am using livewire and the fetch api is being used in javascript web worker. it is compiled by npm .

1 like
binggle's avatar

@vincent15000 Ah. I am using livewire for very simple job.

I have to upload very large size file. like 2~5G.

Network speed between my laptop and server is very poor.

So I am trying to use web worker threading on frontend side. Livewire does not support this.

I am slicing big file into a few hundred pieces. and I want to using fetch to upload file and data.

Actually I might use XMLHttpRequest but it looks old style.

Even though I don't know the benefits of fetch api I would like to use it. It is newer api. And It is worth to know how to use fetch api correctly with Laravel.

Anyway Liveiwire is available for simple job only. Livewire can not be answer for my case.

Thank for concerns.

1 like
vincent15000's avatar

@binggle Ok so the API allows you to slice the files. I never did something like this.

Begin to do something very simple like that.

body: { name: 'John' }

And check if you retrieve this data in the request.

This will allow you to know if the problem is around the use of fetch or around the use of FormData.

Snapey's avatar

using browser network tools you can inspect if data is actually being sent

1 like

Please or to participate in this conversation.