Chase
Chase

Reputation: 135

Get Content From URL with Javascript

I've started a small project that I planned on just using PHP for, but I've ended up needing to integrate javascript with.

I was hoping someone could confirm if this is possible to do with javascript.

Basically I need to get the content from a specific URL, we'll call it:

http://example.com/api/API-DETAILS

That URL will respond with a number. If the number is over 0, then I want to simply run a PHP file, we'll call it:

http://website.com/file.php?data=test&more=example

If the number isn't above 0, nothing else needs to be done.

Is this something that could easily be done in Javascript?

Upvotes: 1

Views: 5025

Answers (1)

entio
entio

Reputation: 4263

Use XHR (XMLHttpRequest):

function reqListener () {
  if (this.status==200)
{
      if(this.responseText !== '0') {
         location="http://website.com/file.php?data=test&more=example";
      }
}
  else
      throw new Error(this.statusText);

}

var oReq = new XMLHttpRequest();
oReq.addEventListener("load", reqListener);
oReq.open("GET", "http://example.com/api/API-DETAILS");
oReq.send();

Upvotes: 2

Related Questions