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.)
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.
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.
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"]); ?>
update 2018:
You can only access cross domain with the following 4 condition
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>
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>
Allow-Control-Allow-Origin: *
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With