Marcel Domuta
Marcel Domuta

Reputation: 410

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

Maybe is a strange title but all I want is to do so:

I have an arduino webserver and an webserver

My arduino server can receive data via url like

192.168.1.2?light=1 - light on

192.168.1.2?light=0 - light off

It is working just fine, but the problem is when I put that link anywhere on an website (in button or just normal links) the arduino server is opening in browser, is there possible to just load it using ajax, js or jquery or just simply using html?

Upvotes: 5

Views: 580

Answers (3)

Hamid
Hamid

Reputation: 118

You can try this.

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

Upvotes: 0

Anton
Anton

Reputation: 2656

Assuming you have a webpage with 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;
    });
});

Upvotes: 4

Alexey
Alexey

Reputation: 999

If want to use this link from a webpage then (requires jQuery):

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

Upvotes: 0

Related Questions