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

This may be a strange name, but all I want to do is:

I have arduino webserverandwebserver

Mine arduino servercan receive data through url, for example

192.168.1.2?light=1 - light on

192.168.1.2?light=0 - turn the lights off

It works fine, but the problem is that when I put this link anywhere on the website (in a button or only in regular links), it arduino serveropens in the browser, can you just download it using ajax, jsor jqueryor just using html?

+4
source share
3 answers

Assuming you have a jQuery webpage.

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 default click action.
        return false;
    });
});
+3

-, ( jQuery):

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

.

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

Source: https://habr.com/ru/post/1648218/


All Articles