最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - How can I send an object using axios? - Stack Overflow

programmeradmin2浏览0评论

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 /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?

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?

Share Improve this question edited Dec 8, 2018 at 17:07 nem035 35.5k6 gold badges92 silver badges104 bronze badges asked Apr 14, 2017 at 15:16 WarrioWarrio 1,9135 gold badges29 silver badges46 bronze badges 2
  • 1 I am not sure, but Request failed with status code 500 says that is server side problem – Leguest Commented Apr 14, 2017 at 15:18
  • part of the error was indeed on the php method: I did this instead: public function create(Request $request) – Warrio Commented Apr 14, 2017 at 15:54
Add a comment  | 

2 Answers 2

Reset to default 11

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.

First of all try with params instead of parameters.

Axios rely on promises you might need to add promise polyfill to your code if you want to support old browsers.

Here is sample request, Read official docs for more information.

axios.get('/url', {
    firstName: 'Fred',
    lastName: 'Flintstone'
  })
  .then(function (response) {
    console.log(response);
  })
  .catch(function (error) {
    console.log(error);
  });
发布评论

评论列表(0)

  1. 暂无评论