For jQuery:
// In this example, if you make an ajax request to the following website
var myUrl = 'http://www.geoplugin.net/json.gp?ip=216.58.209.68';
// But if you make it from a browser, then it will work without problem ...
// However to make it work, we are going to use the cors-anywhere free service to bypass this
var proxy = 'https://cors-anywhere.herokuapp.com/';
$.ajax({
// The proxy url expects as first URL parameter the URL to be bypassed
// https://cors-anywhere.herokuapp.com/{my-url-to-bypass}
url: proxy + myUrl,
complete:function(data){
console.log(data);
}
});
Or using the shortcuts of $.get, $.getJSON or $.post :
var myUrl = 'http://www.geoplugin.net/json.gp?ip=216.58.209.68';
var proxy = 'https://cors-anywhere.herokuapp.com/';
var finalURL = proxy + myUrl;
// With the get JSON (frequently used) method
$.getJSON(finalURL, function( data ) {
console.log(data);
});
// With the get method
$.get(finalURL, function( data ) {
console.log(data);
});
// With the post method
$.post(finalURL, function( data ) {
console.log(data);
});