1

Hy there,

I'm implementing a bookmarklet that needs to post some XMLHttpRequests to a php-script which is done via JQuery.

Basically this is looking something like this (just simple examples...):

Javascript:

$.post('https://www.myserver.com/script.php').always(function(data) {
    console.warn('data:', data);
}

script.php:

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

$res["isSession"] = false;
echo json_encode($res);

At first this wasn't working at all because of the null-origin but after I've seen this post I added the "header('Access-Control-Allow-Origin: *');" line.

With this line it's at least working when i execute the script on a locally installed Apache.

Unfortunately as soon as i try to execute it on my online site that is located on a shared host it doesn't work but gives me "XmlHttpRequest error: Origin null is not allowed by Access-Control-Allow-Origin" error message.

On the shared host it's also Apache running. Could this be caused by some Apache-config? If yes, which one?

What else could be the problem and how can i resolve it?

Thanks

ps.: If i explicitly use jsonp as datatype for the jquery-post it's working, but therefore I'd have to rewrite some php files and some other javascripts that use them too. Therefore I'd like to avoid this option if possible...


edit: I just found out that the php file on the online server is inside a directory that is "locked" with a htaccess-file. If i move the php file to a directory that is accessible for everyone i don't get the mentioned error message.

Does anybody now how i can circumvent this problem? Putting the php file to an publicly accessible directory wouldn't be exactly my preferred option...

Community
  • 1
  • 1
suamikim
  • 5,350
  • 9
  • 40
  • 75
  • Did you request the file from a local file or from a local web server? I guess it won't work from local file because it won't have a valid origin – devnull69 Mar 19 '13 at 13:20
  • I always request the file from a web server. So it's always a http-link and no file-link... When i request it from the locally installed Apache Webserver, everything works fine, as soon as i request it from an online available Apache Webserver it's not working anymore... – suamikim Mar 19 '13 at 14:05
  • I don't mean the location of the requested file ... I mean the location of the file with the jQuery code requesting the remote script.php – devnull69 Mar 19 '13 at 14:07
  • The javascript file that posts the request is also located on the webserver. – suamikim Mar 19 '13 at 14:08
  • I just recognized another thing: If i open the bookmarklet from e.g. google.com, the error message says "Origin http://www.google.at is not allowed by Access-Control-Allow-Origin." instead of "Origin null is not allowed by Access-Control-Allow-Origin."... – suamikim Mar 19 '13 at 14:10

0 Answers0