$ .get - No origin allowed

Today I came across a very strange error trying to get the contents of a PHP file on my server using $.get.

This only happens on Safari and Chrome on Mac OS X (Snow Leopard), on Windows it really works on all browsers.

The function is like:

function _fc() {
   $.get("_x_fc.php", { xaction: 'login', xv1: $('#login').attr("value"), xv2: $('#pass').attr("value") }, function (data) {

      if (data=='0') { letItGo=true; $('#loginform').submit(); }
      else ...//Do some other checks
   });
}
  • This is not a local server, this is a web server with an existing domain
  • I am not trying to execute any cross-domain ajax. Both files are in the same directory.

I can not find a solution for this.

Exact error:

XMLHttpRequest cannot load 
http://www.asking1.com/_x_fc.php?xaction=login&xv1=something&xv2=something.
Origin http://asking1.com is not allowed by Access-Control-Allow-Origin.
+5
source share
3 answers

I did it as follows:

$.get("http://<?php echo $_SERVER['HTTP_HOST']; ?>/_x_fc.php", { xaction: 'login', xv1: $('#login').attr("value"), xv2: $('#pass').attr("value") }, function (data) { (...)

You guys are the best. Thank.

0
source

Your answer is in the error message:

XMLHttpRequest cannot load 
http://www.asking1.com/_x_fc.php?xaction=login&xv1=something&xv2=something.
Origin http://asking1.com is not allowed by Access-Control-Allow-Origin.

http://www.asking1.com http://asking1.com , . . .

URL-, , http://asking1.com, http://askign1.com/_x_fc.php, .

, . , .

, . -, -, , www.asking1.com asking1.com.

+2

wwwis technically a subdomain. So you are breaking same-origin policy. You can solve this problem by installing

function _fc() {
   document.domain = "www.asking1.com";
   $.get("_x_fc.php", { xaction: 'login', xv1: $('#login').attr("value"), xv2: $('#pass').attr("value") }, function (data) {

      if (data=='0') { letItGo=true; $('#loginform').submit(); }
      else ...//Do some other checks
   });
}

or you can fully define your URL that you pass as part of your AJAX request to make sure it is the same.

+2
source

All Articles