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
1 Answer
Reset to default 4Yes, 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
版权声明:本文标题:javascript - Need to implement html button to access REST get resource - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1743996452a2573076.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论