简体   繁体   中英

How send parameters with URL in react?

I send a parameter with this

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

but I need to send parameters like

apiUrl = SERVER_API_URL + '/api/import/estimatee/' + this.state.estimateId + this.state.purchaseOrder.purchaseOrderName + this.state.purchaseOrder.purchaseOrderCurr

How can I solve this Thanks for your feedback people?

Use template literals with query parameters ? and & to include multiple parameters:

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

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

apiUrl will now be:

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

Then you can use axios to post to apiUrl :

axios.post(apiUrl, formData, config);

OR

just paste the parameters in axios's options -- these parameters should be appended to formData , especially if they are relevant to what's being submitted in 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}`;

im solved this way thanks guys.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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