Javascript 需要实现html按钮来访问REST get资源
对于REST和web应用程序来说是相当新的,并且希望创建一个带有按钮和结果显示位置的前端站点 我的REST API结构如下: 浏览时返回一个值 现在我想实现一个GUI,这样当您浏览到c:\resourcebutton.html时 将有一个按钮,当我单击它时,它将调用RESTAPI资源并返回结果。如果我正确理解REST,它应该是这样工作的 我有一个html代码:Javascript 需要实现html按钮来访问REST get资源,javascript,html,rest,Javascript,Html,Rest,对于REST和web应用程序来说是相当新的,并且希望创建一个带有按钮和结果显示位置的前端站点 我的REST API结构如下: 浏览时返回一个值 现在我想实现一个GUI,这样当您浏览到c:\resourcebutton.html时 将有一个按钮,当我单击它时,它将调用RESTAPI资源并返回结果。如果我正确理解REST,它应该是这样工作的 我有一个html代码: <!DOCTYPE html> <html> <body> <p>Click the
<!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>
单击按钮以触发一个函数
点击我
函数myFunction(){
document.getElementById(“demo”).innerHTML=“Hello World”;
}
如何以及在何处插入GET方法来调用API?使用Javascript是常见的吗?是的,您必须使用Javascript。事实上,您需要Ajax 为了简化工作,您应该下载JQuery并将其包含到您的站点中,然后使用如下内容:
$.post( "http://hostserver.com/MEApp/MEService", function( data ) {
document.getElementById("demo").innerHTML = data;
//Or, in the JQuery-way:
$('#demo').html(data);
});
<!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.com/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>
jQuery源代码可以在以下位置找到:
您的html文件将如下所示:
$.post( "http://hostserver.com/MEApp/MEService", function( data ) {
document.getElementById("demo").innerHTML = data;
//Or, in the JQuery-way:
$('#demo').html(data);
});
<!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.com/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>
//通常,您会将脚本标记放入头部
函数myFunction(){
//这将执行POST请求。
//使用“$.get();”执行get请求(如果不确定需要什么,必须查看rest API文档)
//浏览器从给定的url下载网页,并返回数据。
$.post(”http://hostserver.com/MEApp/MEService,函数(数据){
//一旦浏览器完成下载,就会调用此函数。
$('#demo').html(数据);
});
}
单击按钮以触发一个函数
点击我
AJAX,是您需要的。例如,您可以使用html和javascript来实现这一点吗?因为我对这些技术比较熟悉。Ajax
是Javascript。Ajax的意思是“异步Javascript请求”,基本上只是HTTP请求的名称,它是在Javascript的帮助下执行的。如果没有Ajax,每次您想从服务器加载内容时,浏览器都需要刷新页面。@maja Ajax似乎与XML和SOAP配合得很好。但我正在使用RESTEasy web服务,希望学习并使用该资源。@JustinBieber Ajax用于执行HTTP请求。您可以下载HTML、XML、JSON、JPG、MP4甚至整个互联网的所有内容。只要您提供了正确的Url、正确的方法(Post、Get等)和正确的参数,它就会工作。这是对javascript的进一步研究,对吗?在代码中使用了$.post…);我想这个块就是jQuery,因为我是jQuery新手。这似乎是一个非常好的解决方案,但对我来说可能是过度劳累,因为我刚刚开始了解它的工作原理,并希望了解如何设置调用rest资源并显示结果的按钮的格式。谢谢。这是一个JavaScript库,它提供了许多跨浏览器兼容的函数,这使生活更加轻松。JQuery在Internet上的许多站点上都使用,我建议在每个JavaScript中都使用它-Project@JustinBieber请参阅我更新的示例。使用JQuery修改HTML也容易得多。我对web应用程序完全陌生,希望了解REST api和web gui之间的交互 单击按钮触发函数。单击我函数myFunction(){document.getElementById(“demo”).innerHTML=“Hello World”;}$.post(“”,函数(数据){document.getElementById(“demo”).innerHTML=data;//或者,以JQuery的方式:$('.\demo').html(数据);});你的意思是这样吗?在我的例子中,我需要使用GET,因为服务中只实现了GET方法。它是这样的:@Path(“/MEService”)公共类MEService{@GET()/@Path(“/”@products”(“text/plain”)公共字符串getServiceInfo(){return“50”;}