I made a 'voting' API for a topsite for the sites registered to use.
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.7.1/jquery.min.js"></script>
<script type="text/javascript">
var id = 1;
$(document).ready(function(){
$.getJSON("http://mysite.com/index.php?page=vote", { id: id, hasVoted: 'unknown' }, function(data) {
if(data == 2) {
window.location.replace("http://mysite.com/index.php?page=vote&id=" + id);
}
});
});
</script>
Basically, I give the user's their ID and then they put that code in their files. The site returns a number and upon that, it redirects the user or not to voting.
So, testing on localhost that code throws this error:
XMLHttpRequest cannot load http://mysite.com/index.php?page=vote&id=1&hasVoted=unknown. Origin http://localhost is not allowed by Access-Control-Allow-Origin.
If I use this code:
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.7.1/jquery.min.js"></script>
<script type="text/javascript">
var id = 1;
$(document).ready(function(){
$.getJSON("http://mysite.com/index.php?page=vote&callback=?", { id: id, hasVoted: 'unknown' }, function(data) {
if(data == 2) {
window.location.replace("http://mysite.com/index.php?page=vote&id=" + id);
}
});
});
</script>
Then it does nothing. It throws no error. ( While data is supposed to be equal to 2 ( When checking that URL directly, it returns 2 ) ).
And, if I try to do a little test, and do alert(data);
It throws nothing still.
I'm completely clueless about this. Anything will help.