Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to get data with JavaScript from another server?

Tags:

How can I make requests to other server(s) (i.e. get a page from any desired server) with a JavaScript within the user's browser? There are limitations in place to prevent this for methods like XMLHttpRequest, are there ways to bypass them or other methods?

That is a general question, specifically I want to check a series of random websites and see if they contain a certain element, so I need the HTML content of a website without downloading any additional files; all that in a JavaScript file, without any forwarding or proxy mechanism on a server.

(Note: one way is using Greasemonkey and its GM_xmlhttpRequest.)

like image 437
user46665 Avatar asked Feb 23 '09 15:02

user46665


People also ask

Can JavaScript fetch data from network?

It can send network requests to the server and load new information whenever it's needed, without reloading the browser. Syntax: The fetch() method only has one mandatory argument, which is the URL of the resource you wish to fetch.

Can JavaScript connect to a server?

JavaScript is a client-side language that runs in the browser and MySQL is a server-side technology that runs on the server. You need to use the server-side language Node. js to connect to the MySQL Database. Node.


2 Answers

You should check out jQuery. It has a rich base of AJAX functionality that can give you the power to do all of this. You can load in an external page, and parse it's HTML content with intuitive CSS-like selectors.

An example using $.get();

$.get("anotherPage.html", {}, function(results){   alert(results); // will show the HTML from anotherPage.html   alert($(results).find("div.scores").html()); // show "scores" div in results }); 

For external domains I've had to author a local PHP script that will act as a middle-man. jQuery will call the local PHP script passing in another server's URL as an argument, the local PHP script will gather the data, and jQuery will read the data from the local PHP script.

$.get("middleman.php", {"site":"http://www.google.com"}, function(results){   alert(results); // middleman gives Google's HTML to jQuery }); 

Giving middleman.php something along the lines of

<?php    // Do not use as-is, this is only an example.   // $_GET["site"] set by jQuery as "http://www.google.com"   print file_get_contents($_GET["site"]);  ?> 
like image 82
Sampson Avatar answered Oct 15 '22 05:10

Sampson


update 2018:

You can only access cross domain with the following 4 condition

  • in response header has Access-Control-Allow-Origin: *

Demo

$.ajax({    url: 'https://api.myjson.com/bins/bq6eu',    success: function(response){      console.log(response.string);    },    error: function(response){      console.log('server error');    }  })
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script>
  • use server as bridge or proxy to the target

Demo:

$.ajax({    url: 'https://cors-anywhere.herokuapp.com/http://whatismyip.akamai.com/',    success: function(response){      console.log('server IP: ' + response);    },    error: function(response){      console.log('bridge server error');    }  })
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script>
  • using browser addon to enable Allow-Control-Allow-Origin: *
  • disable browser web security

Chrome

chrome.exe --args --disable-web-security 

Firefox

about:config -> security.fileuri.strict_origin_policy -> false 

end


noob old answer 2011

$.get(); can get data from jsbin.com but i don't know why it can't get data from another site like google.com

$.get('http://jsbin.com/ufotu5', {},   function(results){  alert(results);  }); 

demo: http://jsfiddle.net/Xj234/ tested with firefox, chrome and safari.

like image 44
ewwink Avatar answered Oct 15 '22 04:10

ewwink