我有一个类似于以下的按钮:
<button onclick="window.location='http://www.example.com';">Visit Page Now</button>
我希望单击按钮将请求发送到端点,但不打开新窗口或新选项卡。这可能吗?
最佳答案
您可以使用 JavaScript 来发出请求。您必须稍微更改一下标记
<button onclick="hitEndpoint('http://www.example.com')">Visit Page Now</button>
然后在页面的脚本部分,您可以编写如下内容:
function hitEndpoint(url){
var xhttp = new XMLHttpRequest();
xhttp.onreadystatechange = function(){
//we're done making the request
if(this.readyState === 4) {
//status code was successful
if(this.status === 200) {
console.log("hit endpoint successfully")
} else {
console.log("error hitting endpoint");
}
}
};
xhttp.open("GET", url);
xhttp.send();
}
关于javascript - HTML 按钮链接 : Hit endpoint but do not open page,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54099808/