views:

37

answers:

3

Hi! I'm trying to use an ajax request to connect, and gather data from, a PHP file. THe AJAX JS is on a different website than the PHP, just an FYI.

Here is the JS:

var quer;
 try
 {
  quer = new XMLHttpRequest();//I'm running in safari, so this gets called.
 } 
 catch (e)
 {
  try
  {
   quer = new ActiveXObject("Msxml2.XMLHttp");
  }
  catch (e)
  {
   try
   {
    quer = new ActiveXObject("Microsoft.XMLHttp");
   }
   catch (e)
   {
    return false;
   }
  }
 }
 quer.onreadystatechange = function(){
  if (quer.readyState == 4)//Good to go.
  {
   var resp = quer.responseText;
   alert(resp);
  }
 }
 quer.open("POST", "(blanked URL for security reasons)", true);
 quer.send(null); 

Resp is always, and I mean ALWAYS blank. Can anyone offer any help?

A: 

you cannot make ajax requests to scripts that reside on other domains.

Crayon Violent
Are you sure? I'm perfectly able to SEND data to the PHP file I'm requesting. I just can't receive its contents. -- Thanks a million for the quick reply! (P.S. I would vote you up one if I were allowed to)
Flynn
yes, it is a violation of the same origin policy. Others have posted links to that in their answers, as well as other SO questions that are similar.
Crayon Violent
+1  A: 

THe AJAX JS is on a different website than the PHP

There is your problem. You can't do an XMLHttp request from a different domain.

You can read more about the same origin policy.

Guffa
Allright. I've moved the JS file to the same domain as the PHP file. I'm now referencing the JS file on the other domain. I'm still getting a blank -- Am I still breaking the Same Origin Policy, even though both scrips are on the same site?
Flynn
@Flynn: Yes, you are still doing the request from a different domain. The js file runs in the scope of the page where you load it, not where you load it from.
Guffa
A: 

see here for quite a bit more detail

PurplePilot