我需要使用带参数的网址作为示例:
https://domain.pl/ptpdf-gen?selected_posts=4871&advisor=magda,wojciech
但是axios响应是这样的对象:
{"https://domain.pl/ptpdf-gen?selected_posts":"4871","advisor":"magda,wojciech"}
如何通过axios将url作为字符串发送?
答案 0 :(得分:0)
var selected = 4871
var advisor = ["magda","wojciech"]
axios.post('https://domain.pl/ptpdf-gen', {selected, advisor })
答案 1 :(得分:0)
可选地,上述请求也可以按以下方式完成
axios.get('/user', {
params: {
selected_posts: 4871
advisor: ["magda", "Wojciech"]
},
paramsSerializer: params => {
return qs.stringify(params)
}
})
.then(function (response) {
console.log(response);
})
.catch(function (error) {
console.log(error);
})
.then(function () {
// always executed
});
qs是一个外部库,
答案 2 :(得分:0)
因此,我使用URLSearchParams这样分割了网址:
const currHref = window.location.search;
const urlParams = new URLSearchParams(window.location.search);
const myParam = urlParams.get('selected_posts');
const myParam2 = urlParams.get('advisor');
然后使用axios.post我可以发送参数:
axios.post("http://domain.pl/create", {myParam, myParam2})
在服务器上,我确实处理了以下参数:
const state = req.body;
const stateValues = Object.values(state);
然后我可以用stateValues [0]和stateValues [1]合并网址;
let linkUrl = "https://domain.pl/ptpdf-gen?selected_posts=" + stateValues[0] + "&advisor=" + stateValues[1];
工作。