1

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?

Sergio
  • 28,539
  • 11
  • 85
  • 132
Flynn
  • 11
  • 2
  • httpxmlrequest does not work cross domain. So if your js is loaded on a page and the page domain does not correspond to the php domain, you get zilch. – BGerrissen Sep 11 '10 at 18:39
  • possible duplicate of [Empty responseText from XMLHttpRequest](http://stackoverflow.com/questions/1941340/empty-responsetext-from-xmlhttprequest) – Guffa Sep 11 '10 at 19:18

2 Answers2

1

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
  • 687,336
  • 108
  • 737
  • 1,005
  • 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 Sep 11 '10 at 19:20
  • @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 Sep 11 '10 at 19:37
0

You cannot make AJAX requests to scripts that reside on other domains. It is a violation of the same origin policy.

bfavaretto
  • 71,580
  • 16
  • 111
  • 150
CrayonViolent
  • 32,111
  • 5
  • 56
  • 79
  • 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 Sep 11 '10 at 18:48
  • 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. – CrayonViolent Sep 11 '10 at 18:53