简体   繁体   中英

Server side CURL POST request on button press

I have some code which generates a token.

Currently I have to take the token and manually execute the POST request to communicate with the 3rd party endpoint and complete the action.

I would like to trigger this automatically on the press of the button.

I can write a curl command to execute the request but I am not sure I can embed that in my html page.

I know to place it in the script after receiving a successful token but then i imagine I need to store the token in a variable also so I can feed it into the curl command.

some help and guidance would be much appreciated.

Thanks

Expanding on @ozgur comment with an example:

// This will go in your <script> tag
// or wherever you JavaScript code is handling the button press

  fetch('http://example.com/movies', {
    method:'POST'
  })
  .then(response => response.json())
  .then(data => {
    window.localStorage.setItem('myCustomToken', data.token);
    // Can be access later like so:
    window.localStorage.getItem('myCustomToken');
  })
  .catch(err => console.error(err));

You can see more examples and details about the Fetch API in the Mozilla Doc

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