如何在URL中发送带有URL的参数?

时间:2018-10-06 20:13:44

标签: javascript reactjs

我以此发送参数

apiUrl = SERVER_API_URL + '/api/import/estimatee/' + this.state.estimateId;
.post(apiUrl, formData, config)

但是我需要发送

之类的参数
apiUrl = SERVER_API_URL + '/api/import/estimatee/' + this.state.estimateId + this.state.purchaseOrder.purchaseOrderName + this.state.purchaseOrder.purchaseOrderCurr

我该如何解决?谢谢您的反馈?

2 个答案:

答案 0 :(得分:2)

template literals与查询参数?&一起使用以包含多个参数:

const { estimateId, purchaseOrderCurr, purchaseOrderName } = this.state;

const apiURL = `${SERVER_API_URL}/api/import/estimate/order?estimateId=${estimateId}&purchaseOrderName=${purchaseOrderName}&purchaseOrderCurr=${purchaseOrderCurr}`

apiUrl现在将是:

http://serverapi/api/import/estimate/order?estimateId="1234"&purchaseOrderName="orderName"&purchaseOrderCurr="USD"

然后您可以使用axiospostapiUrl

axios.post(apiUrl, formData, config);

OR

只需将参数粘贴到axios的options中-这些参数应附加到formData,特别是如果它们与formData中提交的内容有关

const { estimateId, purchaseOrderCurr, purchaseOrderName } = this.state;

axios.post(`${SERVER_API_URL}/api/import/estimate`, { formData, estimateId, purchaseOrderCurr, purchaseOrderName }, config);

OR

const fd = new FormData();
fd.append('estimateId', estimateId);
fd.append('purchaseOrderCurr', purchaseOrderCurr);
fd.append('purchaseOrderName', purchaseOrderName);
...other formData options

axios.post(`${SERVER_API_URL}/api/import/estimate`, fd, config);

答案 1 :(得分:0)

const { estimateId, purchaseOrderName } = this.state;
const apiUrl = `${SERVER_API_URL}/api/import/estimate/${estimateId}/${purchaseOrderName}`;

我通过这种方式解决了,谢谢。