Rio
Rio

Reputation: 14892

Access-Control-Allow-Origin error with JavaScript

I'm trying to load a bit of content onto sites that I browse to using a simple Javascript bookmarklet. Since I'm hoping to make this usable by anyone and not just me, I make a get request that has a unique identifier associated with the bookmarklet, like so:

$.get("http://www.mysite.com/dothis.php?id=1234", {}, function(data) {
   $("body").append(data);
});

The trouble is that I get

XMLHttpRequest cannot load http://www.mysite.com/dothis.php?id=1234. Origin http://www.google.com is not allowed by Access-Control-Allow-Origin.

The issue seems that I can recognize the request (dothis.php can save the fact that I made the get request to a database, for example) but it cannot return any data back to the requester. It makes sense, but is there some way around it? iFrames won't work because I'll need to act upon that returned data, too, eventually.

Thanks!

Upvotes: 0

Views: 972

Answers (1)

keks
keks

Reputation: 189

In your PHP script you can allow Access-Control-Allow-Origin from every website. Just put

<?php header("Access-Control-Allow-Origin: *"); ?>

to the very beginning of your php file. I haven't tested this, but it ought to work.

Upvotes: 1

Related Questions