PING to a Server using JavaScript without AJAX

Hi

Is possible do a ping to a server and know if the server is online or offline without use AJAX?

I found this code but use AJAX

function pingURL() {
// Getting the URL from the User
var URL = $("#url").val();
var settings = {
// Defining the request configuration
cache: false,
dataType: "jsonp",
crossDomain: true,
url: URL,
method: "GET",
timeout: 5000,
headers: {accept: "application/json", "Access-Control-Allow-Origin": "*",},

// Defines the response to be made
// for certain status codes
statusCode: {
200: function (response) {
document.getElementById("outputDiv").innerHTML="<h3 style='color:green'>Status 200: Page is up!";
},
400: function (response) {
document.getElementById("outputDiv").innerHTML="<h3 style='color:red'>Status 400: Page is down.</h3>";
},
0: function (response) {
document.getElementById("outputDiv").innerHTML="<h3 style='color:red'>Status 0: Page is down.</h3>";
},
},
};
// Sends the request and observes the response
$.ajax(settings).done(function (response) {
console.log(response);
})
.fail(function (response) {
console.log("Error" + response);
});
}

I also other code:

function ping(extServer){
var ImageObject = new Image();
ImageObject.src = "http://"+extServer+";
if(ImageObject.height>0){
alert("Servidor en Linea !");
} else {
alert("Servidor fuera de linea :(");
}

}

Hovever with this last code , the system alway return OffLine


(");

What other option can exist?

Hi @partytecnico welcome to the forums

When you post code you need to format it. You can either highlight all the code and press the </> icon or add a line containing nothing by 3 backticks ``` both before and after the code. I have done it for you this time. :slight_smile:

1 Like

Well the imageobject will have no height if what you make the SRC isnt an image.
If you go back and look at the code you copied, you’ll see they’re pulling an image file from the remote server.

Let’s clarify a few things;

  • You’re not pinging. That’s a specific request type (ICMP), that Javascript cannot mimic. What you’re doing is making an HTTP request to the server.
  • Your request will be JAX. It may be AJAX, it may be SJAX (Yes, i’m inventing that term).

Why are you so opposed to the AJAX method? It’s the “right” way to do it…

Hi

The system not allow the use of this instructions

// Sends the request and observes the response

$.ajax(settings).done(function (response) {
console.log(response);
})

That’s a jQuery request.

Have you loaded jQuery into your site?

Only allow JS

It needs to be pure JavaScript

In the configuration I only can put a link to a file JavaScript.

I am testing my code in

(function () {
var URL = "https://playcode.io/javascript";
var settings = {
// Defining the request configuration
cache: false,
dataType: "jsonp",
crossDomain: true,
url: URL,
method: "GET",
timeout: 5000,
headers: {accept: "application/json", "Access-Control-Allow-Origin": "*",},

// Defines the response to be made
// for certain status codes
statusCode: {
200: function (response) {
document.getElementById("outputDiv").innerHTML="<h3 style='color:green'>Status 200: Page is up!";
},
400: function (response) {
document.getElementById("outputDiv").innerHTML="<h3 style='color:red'>Status 400: Page is down.</h3>";
},
0: function (response) {
document.getElementById("outputDiv").innerHTML="<h3 style='color:red'>Status 0: Page is down.</h3>";
},
},
};

         $.ajax(settings).done(function (response) {
            console.log(response);
         })
         .fail(function (response) {
            console.log("Error" + response);
         });
      


})();

And this code generate this error JavaScript Playground (playcode.io)

ReferenceError: $ is not defined
at :30:3
at :35:3
at mn (:16:5455)

Yes, because $ is a jQuery reference. So it wont be defined if you dont have jQuery loaded.

If you’re trying to do the ajax request purely in vanilla JS;

const myRequest = new Request(aURLgoeshere);
fetch(myRequest).then((response) => {
  if (response.status == 200) { //Do whatever you were gonna do for a 200. }
  else { //Do something else. }
});

Hi
I have done this but not work

Regards

Looks like you’re bouncing off the CORS of the server you’re trying to check. This is not unexpected. If the target URL’s CORS does not allow cross-origin requests, the fetch will fail.

If you’re purely trying to figure out if a server exists, you can no-cors the request;

fetch("http://google.com",{mode: "no-cors"}).then((response) => {
  console.log("Online");
}).catch(e => { console.log("Offline")});

Note that you must get the protocol correct; The above will tell you Offline. But changing the url to https://google.com will tell you it’s Online.

This will NOT tell you if a page exists or not; it will simply tell you it found a server on the other end of its request.

Hi

I need check if the page is available.

For my requirement. I have done the same test with VPN active / inactive and the response is Online

You wont be able to go around the CORS of the target site for that.

How can do for know if a URL is available ?

Something other than Javascript. cURL via PHP, a local script running on the server machine… uhhh…

Hi m_hutley

We use a card that when the enduser press this card the system call the code JS

However this only work when the user upload the page the first time, if press the card new this follow with the same value of return

How can avoid this?

Regards

This topic was automatically closed 91 days after the last reply. New replies are no longer allowed.