简体   繁体   English

axios post数组数据

[英]axios post array data

I'm trying to send post request to a server I don't have much control on it.我正在尝试向我没有太多控制权的服务器发送发布请求。 The only thing I know is I can obtain the correct response if I post the following data in Postman我唯一知道的是,如果我在 Postman 中发布以下数据,我可以获得正确的响应

x-www-form-urlencoded radio button checked

Entered the following 2 array data:
    product_id_list[]          pid1234
    product_id_list[]          pid1235

Header - Content-Type: application/x-www-form-urlencoded

Method: Post

But when I tried to do it through axios, it doesn't seems the correct params data can get through.但是当我尝试通过 axios 进行操作时,似乎无法通过正确的参数数据。 I've tried我试过了

axios.post('https://test.com/api/get_product,
    querystring.stringify({
      'product_id_list': ['pid1234', 'pid1235']
    }))
.
.
.
axios.post('https://test.com/api/get_product,
    querystring.stringify({
      'product_id_list[]': 'pid1234',
      'product_id_list[]': 'pid1235'
    }))
.
.
.

Anyone got an idea on how to translate this type of array data in axios?有人知道如何在 axios 中翻译这种类型的数组数据吗?

You can use the native axios to create a request. 您可以使用本机axios创建请求。 You can pass your payload with the data key. 您可以使用data密钥传递有效负载。

 import axios from 'axios'; let payload = { product_id_list: ['pid1234', 'pid1235'] }; axios({ url: 'https://test.com/api/get_product', method: 'post', data: payload }) .then(function (response) { // your action after success console.log(response); }) .catch(function (error) { // your action on error success console.log(error); }); 

You can try running your axios code from your browser here . 您可以在此处尝试从浏览器运行axios代码。

you can send your post request with 'Content-Type': 'application/json' header (if server can handle it)您可以使用'Content-Type': 'application/json' header 发送您的发布请求(如果服务器可以处理)

const productList = [1, 2, 3, 4]

const data = JSON.stringify({product_list: productList})
const config = {
    headers: {'Content-Type': 'application/json'}
}

return axios.post('api/url', data, config)

This issue popped up for me as well, and I solved it with new FormData() . 这个问题也对我突然出现,我使用new FormData()解决了这个问题。

Having an array: 有一个数组:

const product_id_list = ['pid1234', 'pid1235']

const bodyFormData = new FormData();

product_id_list.forEach((item) => {
    bodyFormData.append('product_id_list[]', item);
});

axios.post('https://test.com/api/get_product', bodyFormData)

Doing it this way sent it the request as application/x-www-form-urlencoded, and the proper data in the body. 以这种方式进行操作时,它以application / x-www-form-urlencoded的形式发送了请求,并在正文中发送了适当的数据。

You can try the following: 您可以尝试以下方法:

    var payload = {
            product_id_list: [
                'pid1234',
                'pid1235'
              ]
    };

    axios.defaults.headers.post['Content-Type'] = 'application/x-www-form-urlencoded';
    payloaxios.post('https://test.com/api/get_product', payload)
      .then(function (response) {
        console.log(response);
    })
    .catch(function (error) {
        console.log(error);
    });

Also, you should take a good look to axios documentation . 此外,您还应该仔细阅读axios文档

I think you are missing a single quote symbol after the url: 我认为您在网址后缺少单引号:

axios.post('https://test.com/api/get_product', {
  product_id_list: ['pid1234', 'pid1235']
})
        $.ajax({
            type: "POST",
            url: "proceso.php",
            data: {'array': array,'array2': array2},     
            success: function(data){
                console.log(data);
            }
        });

You can also send an Array of object with FormData in axios -您还可以在 axios 中发送带有 FormData 的 object 数组 -

let full_name = this.state.full_name;
let phone_no = this.state.phone_no;
let carts = [
       { product_id: 1, quantity: 10, ...}, 
       { product_id: 2, quantity: 5, ...}, 
     ]
let orderFormData = new FormData();
orderFormData.append('full_name', full_name);
orderFormData.append('phone_no', phone_no);

//Just stringify carts array
orderFormData.append('carts', JSON.stringify(carts));

const headers = {
  'Content-Type' : 'application/json',
  'Accept' : 'application/json',
};

axios.post(url, orderFormData, {headers: headers})
  .then(function (res) {
    console.log(res);
  })
  .catch(function (error) {
    console.log(error)
  })

And from Backend you can get it from your request.从后端你可以从你的请求中得到它。 if you are using Laravel then you can get cart data with -如果您使用的是 Laravel,那么您可以通过以下方式获取购物车数据 -

json_decode($request->carts); json_decode($request->carts);

this will return you exact array what you send with axios.这将返回您使用 axios 发送的确切数组。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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