Yogesh Chavan
Dec 30, 2020

--

@sravanchowdary.k Thanks for valuable feedback.

Regarding your issue, the URLSearchParams does not return keyvalue pairs as a query string. You need to use it like this:

const params = new URLSearchParams(window.location.search);

console.log(params.get('id')); // 2000

considering you're passing the query params as ?id=2000.

In your case there is no need of using URLSearchParams.

The axios.post requires a stringified data which you can get directly from window.location.search and call it like this:

axios

.post(

"https://my-json-server.typicode.com/typicode/demo/posts",

JSON.stringify(window.location.search.slice(1))

)

.then((response) => console.log("response", response.data))

.catch((error) => console.log(error.message));

In this code, location.search returns query parameter along with question mark so we're using slice to get the params without question mark.

You can find the codesandbox for working demo here: https://codesandbox.io/s/restless-fast-oibvj?file=/src/App.js

--

--

Yogesh Chavan
Yogesh Chavan

No responses yet