admin管理员组

文章数量:1355535

Fairly new to REST and web application and would like to create a front-end site with a button and a place where a result is displayed.

I have REST API structured like this:

This returns a value when browsing to it.

Now I would like to implement a GUI so that when you browse to c:\resourcebutton.html There will be a button and when I click on it, it will call the REST API resource and returns the result. If I understood REST correctly it should work like this.

I have a html code:

<!DOCTYPE html>
<html>
<body>

<p>Click the button to trigger a function.</p>

<button onclick="myFunction()">Click me</button>

<p id="demo"></p>

<script>
function myFunction() {
    document.getElementById("demo").innerHTML = "Hello World";
}
</script>

</body>
</html>

How and where should I insert the GET method to call the API? Is it mon to use Javascript?

Fairly new to REST and web application and would like to create a front-end site with a button and a place where a result is displayed.

I have REST API structured like this: http://hostserver./MEApp/MEService

This returns a value when browsing to it.

Now I would like to implement a GUI so that when you browse to c:\resourcebutton.html There will be a button and when I click on it, it will call the REST API resource and returns the result. If I understood REST correctly it should work like this.

I have a html code:

<!DOCTYPE html>
<html>
<body>

<p>Click the button to trigger a function.</p>

<button onclick="myFunction()">Click me</button>

<p id="demo"></p>

<script>
function myFunction() {
    document.getElementById("demo").innerHTML = "Hello World";
}
</script>

</body>
</html>

How and where should I insert the GET method to call the API? Is it mon to use Javascript?

Share Improve this question asked Jun 19, 2014 at 8:38 JustinBieberJustinBieber 3553 gold badges5 silver badges23 bronze badges 6
  • 1 AJAX, is what you are looking for. You can use for instance jQuery's ajax – idmean Commented Jun 19, 2014 at 8:41
  • Is it not possible to do this with html and javascript? Since I am more familiar with these technology.. – JustinBieber Commented Jun 19, 2014 at 8:51
  • Ajax IS Javascript. Ajax means "Asynchronous Javascript Request" and is basically just the name for a HTTP-Request, which is perfomed with the help of JavaScript. Without Ajax, your browser needs to refresh the page everytime you want to load something from a server. – maja Commented Jun 19, 2014 at 8:53
  • @maja Ajax seemed to be working well with XML and SOAP. But I'm working with RESTEasy web service and would like to learn and consume that resource. – JustinBieber Commented Jun 19, 2014 at 9:03
  • @JustinBieber Ajax is used to perform an HTTP-Request. You can download everything from HTML, XML, JSON, JPG, MP4 or even the whole Internet. As long as you offer the correct Url, the correct Method (Post, Get, ...) and the correct Parameters, it works. – maja Commented Jun 19, 2014 at 9:06
 |  Show 1 more ment

1 Answer 1

Reset to default 4

Yes, you have to do this with JavaScript. In fact, you need Ajax.

To simplify things, you should download and include JQuery to your site, and then use something like this:

$.post( "http://hostserver./MEApp/MEService", function( data ) {
  document.getElementById("demo").innerHTML = data;
  //Or, in the JQuery-way:
  $('#demo').html(data);
});

The jQuery Source can be found here: http://code.jquery./jquery-2.1.1.js

Your html-file would then look like this:

<!DOCTYPE html>
<html>
<head>
    <script src="the/Path/To/The/Downloaded/JQuery.js"></script>
    <script>
        //Usually, you put script-tags into the head
        function myFunction() {
            //This performs a POST-Request.
            //Use "$.get();" in order to perform a GET-Request (you have to take a look in the rest-API-documentation, if you're unsure what you need)
            //The Browser downloads the webpage from the given url, and returns the data.
            $.post( "http://hostserver./MEApp/MEService", function( data ) {
                 //As soon as the browser finished downloading, this function is called.
                 $('#demo').html(data);
            });
        }
    </script>
</head>
<body>

    <p>Click the button to trigger a function.</p>

    <button onclick="myFunction()">Click me</button>

    <p id="demo"></p>    
</body>
</html>

本文标签: javascriptNeed to implement html button to access REST get resourceStack Overflow