[英]How send parameters with URL in react?
我用這個發送參數
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
我該如何解決?感謝您的反饋。
使用帶有查詢參數的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"
然后,您可以使用axios
post
到apiUrl
:
axios.post(apiUrl, formData, config);
要么
只需將參數粘貼到axios的options
- 這些參數應附加到formData
,尤其是當它們與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);
const { estimateId, purchaseOrderName } = this.state;
const apiUrl = `${SERVER_API_URL}/api/import/estimate/${estimateId}/${purchaseOrderName}`;
我通過這種方式解決了,謝謝大家。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.