I have a single html page which is deployed in xampp
page which has a script tag where i use XMLHttpRequest
to call a service url to get json data.
This only works when i call the page with http://localhost/mypage
But when i call the same page from another computer http://ipadress/mypage
it throws an error.
“No 'Access-Control-Allow-Origin' header is present on the requested resource”
I tried using JSONP
solution but that didn't work either
Note that i can only manipulate client-side code (javascript
), i have no control on the service i'm calling
Why does it work with localhost
but it doesn't work with ipadress
?
And what are the alternative solution if there are any ?
var xhttp = new XMLHttpRequest();
xhttp.onreadystatechange = function() {
if (this.readyState == 4 && this.status == 200) {
var result = JSON.parse(this.responseText).result;
document.getElementById("data").innerHTML = result + "%";
}
};
var url = "www.url.com";
xhttp.open("GET", url + "/data.json", true);
xhttp.setRequestHeader("content-type", "application/json");
xhttp.send();