Reputation: 47
Im doing a GET request on a webpage and it is not working because “No ‘Access-Control-Allow-Origin header is present on the requested resource”, which I think means I need the header. How would I set this header on my site in my jquery. My code is as follows:
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script>
<title>Sample Page</title>
<script>
var settings = {
"async": true,
"crossDomain": true,
"url": "https://games.roblox.com/v1/games?universeIds=140239261",
"method": "GET"
}
$.ajax(settings).done(function (response) {
console.log(response);
var content = response.data.playing;
$("#counter").append(content);
});
</script>
</head>
<body>
<h1>Sample Page</h1>
<div id="counter">playing: </div>
</body>
</html>
Thanks in advance for helping. I could not find a way to do this with jquery (atleast how my code is set up), so please help! (Im a beginner in javascript so try to make the answer simple.)
Upvotes: 0
Views: 285
Reputation: 676
You could do something like this
$.ajax({
url: 'https://games.roblox.com/v1/games?universeIds=140239261',
dataType: 'jsonp', // This alone should also work without the beforeSend below
beforeSend: function(xhr){
xhr.setRequestHeader('Access-Control-Allow-Origin', '*');
}
})
However, even this would only work if the server cross origin requests
Upvotes: 0
Reputation: 1710
When you receive this error,it actually means that you are trying to send request to a different domain than your page is on,based on your server side technology you should enable CORS
on your server to accept request from other script that your server has not generated.
Upvotes: 1