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


How to acces an external link without opening it, just loading it

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

我有arduino webserverwebserver

我的arduino server可以像一样通过url接收数据

192.168.1.2?light=1-亮起

192.168.1.2?light=0-熄灭

它运行得很好,但问题是,当我把链接放在arduino server在浏览器中打开的网站上的任何地方(按钮或普通链接)时,是否可以使用ajaxjsjquery加载它,或者只是使用html加载它?

假设您有一个带有jQuery的网页。

HTML

<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>

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;
    });
});

如果要从网页使用此链接,则(需要jQuery):

$.get('http://192.168.1.2?light=1', function(response) {
  console.log(response);
});

你可以试试这个。

$(".yourAtag").click(function(e){
    e.preventDefault();
    $.ajax({url: this.href, success: function(result){
        alert('success!');
    }});
});