猿问

如何从JavaScript调用REST Web服务API?

我有一个带有按钮的HTML页面。当我单击该按钮时,我需要调用REST Web服务API。我尝试在任何地方在线搜索。毫无头绪。有人可以给我牵头/抢先吗?非常感谢。



料青山看我应如是
浏览 1644回答 3
3回答

一只斗牛犬

您的Javascript:function UserAction() {&nbsp; &nbsp; var xhttp = new XMLHttpRequest();&nbsp; &nbsp; xhttp.onreadystatechange = function() {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;if (this.readyState == 4 && this.status == 200) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;alert(this.responseText);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;}&nbsp; &nbsp; };&nbsp; &nbsp; xhttp.open("POST", "Your Rest URL Here", true);&nbsp; &nbsp; xhttp.setRequestHeader("Content-type", "application/json");&nbsp; &nbsp; xhttp.send("Your JSON Data Here");}您的按钮动作::<button type="submit" onclick="UserAction()">Search</button>
随时随地看视频慕课网APP
我要回答