Home > OS >  How do I use a website service to return values into HTML
How do I use a website service to return values into HTML

Time:03-15

I am trying to use a service that runs on my company website in order to grab some information and use it to populate a field in a form.

I have the service URL https://www.test.co.uk/service/17186914/17186914 where the 2 codes are product SKU's. When this services runs it shows a JSON file which contains the Product price. I want to be able to grab this price in order to populate a hidden field on a form I am creating.

Is this possible?

CodePudding user response:

Here is an example of how to fetch json data from a url and display it in an input field.

const url = 'https://jsonplaceholder.typicode.com/todos/1';
fetch(url).then(res => res.json()).then(data => {
  const inputElem = document.querySelector('input');
  inputElem.setAttribute('value', data.title);
  console.log(data);
});
<input />

CodePudding user response:

I roughly understood what are you trying to do.

      const newVariableThatyouNeed = JSON.parse(yourData);

Note that yourData come from the reducer (if you use redux) or the result of your API. And add a new cons with whatever name you want.

  • Related