Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I send an object using axios?

Is there a way to send an object to an API using axios?

This the code I use:

axios.get('/api/phones/create/', {
    parameters: {
        phone: this.phone
    }
})
    .then(response => {
        console.log(response.data)
    })
    .catch(function (error) {
        console.log(error)
    })

on the php side, I have the following:

public function create($phone)
{
    return $phone;
}

I get the following error:

GET http://crm2.dev/api/phones/create 500 (Internal Server Error)
dispatchXhrRequest @ app.6007af59798a7b58ff81.js:256
xhrAdapter @ app.6007af59798a7b58ff81.js:93
dispatchRequest @ app.6007af59798a7b58ff81.js:662
app.6007af59798a7b58ff81.js:2266 Error: Request failed with status code 500
    at createError (app.6007af59798a7b58ff81.js:600)
    at settle (app.6007af59798a7b58ff81.js:742)
    at XMLHttpRequest.handleLoad (app.6007af59798a7b58ff81.js:158)

If I try, axios.get('/api/phones/create/hello') I get hello in the console log.

Is there a way to do this?

like image 895
Warrio Avatar asked Apr 14 '17 15:04

Warrio


People also ask

How do you send an object in Axios post?

First, it needs the URI of the service endpoint. Second, an object which contains the properties that we want to send to our server should be passed to it. Passing a data object as a parameter to the post method is optional; in this way, a post method is very similar to the get method.

Can we send data in Axios get request?

A GET request can be made with Axios to “get” data from a server. The HTTP get request is performed by calling axios. get() . The get() method requires two parameters to be supplied to it.

How do you send a body request in Axios?

Making a POST request in Axios requires two parameters: the URI of the service endpoint and an object that contains the properties you wish to send to the server. For a simple Axios POST request, the object must have a url property. If no method is provided, GET will be used as the default value.

How do you send data in Axios POST request in react?

querySelector("form"); if (form) { form. addEventListener("submit", (e) => { e. preventDefault(); const formData = new FormData(form); axios . post("/update-profile", formData, { headers: { "Content-Type": "multipart/form-data", }, }) .


1 Answers

It depends on what you mean by "send an object".

Since you're using a GET request and passing the object in the parameters, you can serialize it into query params as part of the GET request. This wouldn't really send the object but rather use it to build the query section of the URL for the GET request.

For example, here's how you can make a request to /api/phones/create?phone=123:

axios.get('/api/phones/create/', {
    params: {
        phone: '123'
    }
})

If you want to actually send the object as a serialized JSON to your API, you can use a POST or a PUT request, depending on the semantics of your API.

For example, to send { "phone": "123" } to your api, you could do:

axios.post('/api/phones/create/', {
  phone: '123'
});

Note: axios expects the key params for parameters.

like image 120
nem035 Avatar answered Sep 29 '22 13:09

nem035