如何访问外部链接而不打开它,只需加载它

Mar*_*uta 5 javascript php ajax url jquery

也许是一个奇怪的标题,但我只想这样做:

我有一个arduino webserver和一个webserver

我可以通过类似的arduino server方式接收数据url

192.168.1.2?light=1- 点亮

192.168.1.2?light=0- 关灯

它工作得很好,但问题是,当我将该链接放在网站上的任何位置(在按钮或普通链接中)时,arduino server在浏览器中打开,是否可以使用 加载它ajaxjs或者jquery只是简单地使用html

Ant*_*ton 4

假设您有一个带有 jQ​​uery 的网页。

超文本标记语言

<a class="access-only" href="http://192.168.1.2?light=1">Turn on the light</a>
<a class="access-only" href="http://192.168.1.2?light=0">Turn off the light</a>
Run Code Online (Sandbox Code Playgroud)

JS

$(document).ready(function() {
    // Attach click handler to all "access-only" links.
    $('a.access-only').click(function() {
        // Once the link is clicked, access its URL with a GET request.
        $.get($(this).attr('href'), function(response) {
            // Do nothing here, the URL has been accessed.
        });

        // Return false to prevent the browser's default click action.
        return false;
    });
});
Run Code Online (Sandbox Code Playgroud)