2017-07-13 115 views
1

我試圖發送請求到服務器我沒有太多的控制權。我唯一知道的是我能得到正確的響應,如果我在郵差axios發佈數組數據

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 

發佈以下數據但是,當我試圖通過愛可信做到這一點,它似乎並不正確PARAMS數據可以打通。我試過

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' 
    })) 
. 
. 
. 

任何人都知道如何在axios中轉換這種類型的數組數據?

回答

0

你可以嘗試以下方法:

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); 
    }); 

此外,你應該好好看看axios documentation

0

我認爲你是在URL後缺少一個單引號符號:

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

您可以使用本機axios創建的請求。您可以通過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); 
 
});

你可以嘗試從您的瀏覽器here運行你的愛可信代碼。