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

Forkan's avatar

vue axios response return empty formdata post http request is working fine

Axios code:

updateCategory(id){
            const data = new FormData();
            data.append('name',this.formUpdate.name);
            data.append('avater',this.formUpdate.imgUrl);
            axios.put('category/'+id,data,{ headers: { 'Content-Type': 'multipart/form-data' } })
            .then(res=>{
                 console.log(res);
            })
        }

Controller code

public function update(Request $request, $id)
    {
        $category = Category::find($id);
        return $request;
    }

console result:

data: Array(0)
length: 0
__proto__: Array(0)
0 likes
7 replies
frankielee's avatar

In your Axios

Add catch for the error

axios.put('category/'+id,data,{ headers: { 'Content-Type': 'multipart/form-data' } })
            .then(res=>{
                 console.log(res);
            }),catch(res=>{
			console.log('error',res);
		});

Controller

You should return the $category instead of $request

public function update(Request $request, $id)
    {
        $category = Category::find($id);
        return $category;
    }

Forkan's avatar

no error return status code is ok but Formdata return empty array. axios post method working fine but in axios put method i get the problem

{data: Array(0), status: 200, statusText: "OK", headers: {…}, config: {…}, …}
config: {url: "category/66", method: "put", data: FormData, headers: {…}, transformRequest: Array(1), …}
data: []
headers: {cache-control: "no-cache, private", connection: "Keep-Alive", content-length: "2", content-type: "application/json", date: "Mon, 14 Sep 2020 07:18:25 GMT", …}
request: XMLHttpRequest {readyState: 4, timeout: 0, withCredentials: false, upload: XMLHttpRequestUpload, onreadystatechange: ƒ, …}
status: 200
statusText: "OK"
__proto__: Object
Forkan's avatar

i need to get the data throw the controller but it return Formdata empty array

frankielee's avatar

How does your route look like in the web,php?

Controller


public function update(Request $request, $id)
  {
	if(blank($id)){
		return "id is empty";
	}
        
$category = Category::findOrFail($id); // will return 404 if category not found
        return $category;
    }


Forkan's avatar

i get the id successfully from the controller but can't get the request data ..because formdata return empty f

frankielee's avatar

How about try to dd($category) and check what will return?

SilenceBringer's avatar

Hi @forkan I'm not sure what you want to have, because you returns request object. Do you want to return input data? try like so

public function update(Request $request, $id)
    {
        $category = Category::find($id);
        return $request->all();
    }

do you want to return updated category data? try like so:

public function update(Request $request, $id)
    {
        $category = Category::find($id);
        return $category;
    }

Please or to participate in this conversation.