Can Ajax work across domains??

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
simonmlewis
DevNet Master
Posts: 4435
Joined: Wed Oct 08, 2008 3:39 pm
Location: United Kingdom
Contact:

Can Ajax work across domains??

Post by simonmlewis »

If this is hosted on the same server as the hosted site, it works (minus the extra http://.....)/.

But if it is hosted on an external site, can you run this cross-domains? The site the text field is being entered on is not hosted on the site that the page in the script is hosted on.

The external site host does not permitted external db links, but would this count? Seeing as it's being passed thru to the action hosted domain?

Code: Select all

<script>
function precheck(str)
{
if (str=="")
  {
  document.getElementById("txtHint").innerHTML="";
  return;
  }
if (window.XMLHttpRequest)
  {// code for IE7+, Firefox, Chrome, Opera, Safari
  xmlhttp=new XMLHttpRequest();
  }
else
  {// code for IE6, IE5
  xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
  }
xmlhttp.onreadystatechange=function()
  {
  if (xmlhttp.readyState==4 && xmlhttp.status==200)
    {
    document.getElementById("srcHint").innerHTML=xmlhttp.responseText;
    }
  }
xmlhttp.open("GET","http://www.domain.co.uk/ajax_addproduct.php?q="+str,true);
xmlhttp.send();
}
</script>
Love PHP. Love CSS. Love learning new tricks too.
All the best from the United Kingdom.
User avatar
requinix
Spammer :|
Posts: 6617
Joined: Wed Oct 15, 2008 2:35 am
Location: WA, USA

Re: Can Ajax work across domains??

Post by requinix »

If the domain names are different then no, it's not possible. Either
a) You write some server-side script that does the work and your AJAX calls that, or
b) The other site provides a JavaScript API for you to use
simonmlewis
DevNet Master
Posts: 4435
Joined: Wed Oct 08, 2008 3:39 pm
Location: United Kingdom
Contact:

Re: Can Ajax work across domains??

Post by simonmlewis »

Bugger - ok.
Or do it in an iframe......
Love PHP. Love CSS. Love learning new tricks too.
All the best from the United Kingdom.
Post Reply