Note: after I wrote this up I understood better that you want to post data to an endpoint. You can use fetch() to do that too.
You'll just use these instructions[
^] to do that.
1. make sure your button is _not_ wrapped inside a <form> element.
<button onclick="getNameValue()">Get Name</button>
This button will fire your getNameValue() JavaScript method.
Now we need t use the
fetch API[
^] to get the data you want.
This will occur asynchronously so the page isn't refreshed but instead the data is just retrieved.
Now, I don't know what domain you are retrieveing data from but you have to retrieve data from the same URL where your page is running or you get a CORS (cross-origin request) error.
function getNameValue(){
fetch('https://www.codeproject.com')
.then(response => response.text())
.then(data => console.log(data));
}
the sample can be run by opening your Browser console (F12 in most browsers) while you are reading this page -- then copy just the 3 fetch lines and paste them on the console and hit <ENTER>.
When you do that you'll see that it fetches the main codeproject page as text()
and then uses that as the data input to a console.log(data) call which will display all the data in the console for you.
Obviously, this fetch call would contain a URL to the domain and specific URL which will return your data.
If you do this properly and return JSON from your endpoint then you can use response.json() which will get your data as JSON (JavaScript Object Notation) that will allow you to easily get your value out.