Page 1 of 1

Can Ajax work across domains??

Posted: Tue Mar 20, 2012 10:25 am
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>

Re: Can Ajax work across domains??

Posted: Tue Mar 20, 2012 11:23 am
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

Re: Can Ajax work across domains??

Posted: Tue Mar 20, 2012 11:25 am
by simonmlewis
Bugger - ok.
Or do it in an iframe......