'How to use JavaScript or Jquery to dynamically changes HTML text input

I have a web service that returns a string. https://localhost:5001/mywebservice

Now, I made a HTML page with a text input. I'd like to use to JavaScript or JQuery to call my webservice. Then update the text input with whatever returns from my web service https://localhost:5001/mywebservice

I tried to use the following JQuery code snippet. But it won't work.

$('button').on('click', function (e) {
    var str = $('https://localhost:5001/mywebservice').val();
    $("#MyString").prop('value', str);
});

I'd appreciate it if someone can give me a hint. Thank you for your help.



Solution 1:[1]

ok, so first of all, you did the event listener right. If this script runs within the HTML file, then you can select elements from the document. Use

$("<select your input>").val() 

if it is a text box or .html() if it is an element. To request the return info of your service, do not use jquery $. it was not built to ajax.

  1. make sure it is always up and running when you use the platform
  2. Instead of hosting it on localhost, try running it on whatever platform this js is on.
  3. make the localhost only display the info you are sending back
  4. send an ajax call to your localhost website, use
$.ajax({
url:"https://localhost:5001/mywebservice",
success:function(d){
//d is your data
}
})

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Hermanboxcar