Press enter to see results or esc to cancel.

Failed to load : No ‘Access-Control-Allow-Origin’ header is present on the requested resource. Origin is therefore not allowed access.

In this video I have shown how you can over come the error,

Failed to load : No 'Access-Control-Allow-Origin' header is present on the requested resource. Origin  is therefore not allowed access.

This error is because of Cross-Origin Resource Sharing (CORS) issue. This simply means that you are trying access information from other domain which is actually not permitted because of cross domain security issue.

I have shown in the video a real time example how I faced this error and how to overcome.
In the video I have explained 2 ways to over come this,
1 . Use JSONP instead of JSON on requesting data from other domain.

The below program code shows how you can use JSONP to overcome CORS issue,

<script src="jquery.min.js"></script>
var ip_address=$("#ip_address").val();

type: "GET",
url: "",
data: {
dataType: "jsonp",
jsonpCallback: "jsonp_callback",
crossDomain: true,
success: function(result) {
error: function(result) {

function jsonp_callback(json){

<h1>Jquery Get Request Test</h1>
IP Address : <input type="text" id="ip_address"/><br>

Download the jquery.min.js here jquery.min

2 . Use Javascript instead of Jquery.

The above code functionality can be implemented using only javascript. The use of javascript only can also help to overcome the CORS issue. The javascript code for getting request is,

<!DOCTYPE html>
window.onload = function() {
document.getElementById("Save").onclick = function fun() {
var x = document.forms["myForm"]["ip_address"].value;
var Url = "" +x;
var xhr = new XMLHttpRequest();'GET', Url, true);
xhr.onreadystatechange = processRequest;
function processRequest(e) {
if (xhr.readyState == 4 && xhr.status == 200) {
// alert(xhr.responseText);
var response1 = JSON.parse(xhr.responseText);
document.getElementById("statusCode").innerHTML = + ", " + response1.statusCode;
document.getElementById("statusCode").innerHTML = response1.statusCode;
document.getElementById("statusMessage").innerHTML = response1.statusMessage;
document.getElementById("ipAddress").innerHTML = response1.ipAddress;
document.getElementById("countryCode").innerHTML = response1.countryCode;
document.getElementById("countryName").innerHTML = response1.countryName;
document.getElementById("regionName").innerHTML = response1.regionName;
document.getElementById("cityName").innerHTML = response1.cityName;
document.getElementById("zipCode").innerHTML = response1.zipCode;
document.getElementById("latitude").innerHTML = response1.latitude;
document.getElementById("longitude").innerHTML = response1.longitude;
document.getElementById("timeZone").innerHTML = response1.timeZone;
Javascript Get Request Test From Form
<br> <br>
<form name="myForm">
<input type="text" name="ip_address"/>
<input type="button" id="Save" onclick="f1()" value="test"/>
<br> <br>
<table border="1">
<tr><td>statusCode :</td><td id="statusCode"></td></tr>
<tr><td>statusMessage :</td><td id="statusMessage"></td></tr>
<tr><td>ipAddress :</td><td id="ipAddress"></td></tr>
<tr><td>countryCode :</td><td id="countryCode"></td></tr>
<tr><td>countryName :</td><td id="countryName"></td></tr>
<tr><td>regionName :</td><td id="regionName"></td></tr>
<tr><td>cityName :</td><td id="cityName"></td></tr>
<tr><td>zipCode :</td><td id="zipCode"></td></tr>
<tr><td>latitude :</td><td id="latitude"></td></tr>
<tr><td>longitude :</td><td id="longitude"></td></tr>
<tr><td>timeZone :</td><td id="timeZone"></td></tr>



Comments are disabled for this post