[Okta Webinar] Learn how to a build a cloud-first strategyRegister Now

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 410
  • Last Modified:

Cross Domain Communication

Hello, I was attempting to use AJAX to send information from only one field in a form to another server.  My reason for using Ajax was because I wanted to send this value without posting the entire form.  I got the code working but later found out that Ajax will not allow me to communicate across my different domains (which is what I'm trying to do).  Is there any way at all for me to do this?  Can I even post two forms to two different domains with one submit button (Ex: have f form but have the data posted to two different servers at the time of submission).  If anything like this is possible, please let me know.

Thanks in advance for your help.
0
jtmerch
Asked:
jtmerch
  • 8
  • 3
  • 3
  • +2
1 Solution
 
0h4crying0utloudCommented:


Not sure this will help you but I when I got a similiar error I fixed with the follow script addition:


if (navigator.appName != "Microsoft Internet Explorer") {
   try { netscape.security.PrivilegeManager.enablePrivilege("UniversalBrowserRead"); }
      catch (e) { alert("Permission UniversalBrowserRead denied.");}
}


Here's the whole example script that uses it:

<script type="text/javascript" language="javascript">

// Error: uncaught exception: Permission denied to call method XMLHttpRequest.open

  var http_request = false;

  function makeRequest(url, parameters) {

   try {
    netscape.security.PrivilegeManager.enablePrivilege("UniversalBrowserRead");
   } catch (e) {
    alert("Permission UniversalBrowserRead denied.");
   }

    http_request = false;
    http_request = new XMLHttpRequest();
    if (http_request.overrideMimeType) {
      http_request.overrideMimeType('text/xml');
    }
    if (!http_request) {
      alert('Cannot create XMLHTTP instance');
      return false;
    }
    http_request.onreadystatechange = alertContents;
    http_request.open('GET', url + parameters, true);
    http_request.send(null);
  }

  function alertContents() {
    if (http_request.readyState == 4) {
      if (http_request.status == 200) {

        var string = http_request.responseText;
      alert(string);

      } else {
        alert('There was a problem with the request.');
      }
    }
  }
  function updateweather() {
    makeRequest('http://www.wunderground.com/auto/rss_full/global/stations/16239.xml', '');
  }
</script>

<input type="button" name="button" value="GET XML"
  onclick="javascript:updateweather();">


Here is a link for more info on what I am reffering to:
http://www.captain.at/howto-ajax-permission-denied-xmlhttprequest.php
0
 
jtmerchAuthor Commented:
Hi, the page says that it cannot be used for cross domain communication.  It says that the page should be run locally.

Here's what it says:

--------------------------------------------------------
Below is an example that can be run locally (save it to your harddisk and open the page with Mozilla/Firefox). It will fetch some RSS XML data from wunderground.com and alert it.

Always remember: XMLHttpRequest needs UniversalBrowserRead!

If the page with the XMLHttpRequest is on a http:// URI (on a webserver), it is not possible to fetch data from another domain!!! This is a security measure of Mozilla/Firefox.
Still, it comes in handy, if you can read remote data from a local application. E.g. you've got a local XUL application that needs to get data from other servers - Dude! Sweet! :-)
---------------------------------------------
0
 
0h4crying0utloudCommented:

Actually I don't think that will work.. according to a PAQ:

http://www.experts-exchange.com/Web/Web_Languages/JavaScript/Q_21869937.html
0
Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
jtmerchAuthor Commented:
Thanks, 0h4.  Any other ideas?
0
 
0h4crying0utloudCommented:


No sorry, I have not run into this question if no answer comes by can will re post it Monday where will be a lot more experts on.
0
 
jtmerchAuthor Commented:
Ok, I'll just wait until then.
0
 
UnexplainedWaysCommented:
Um, iv'e never done it, but here's an idea.

You could pass the value back to your server via AJAX, and then that server side page call's a webservice on the other server.

[Your Site] ---AJAX---> [Your Server] ----Web Service----> [Other server]

I know a -> b works, b -> c works, but ive never done a - > b -> c, but it should work.
0
 
jtmerchAuthor Commented:
Thanks, that's exactly what I'm trying to do and it works locally.  The only problem is that when going from the client on one domain to Ajax to the server which resides on another domain I receive permission denied error because of some feature that doesn't allow javascript for cross browser communication.
0
 
UnexplainedWaysCommented:
So is your page a static html or do you have server side code? (asp/php etc)
0
 
MrHorizontalCommented:
As UnexplainedWays says, you'll need to make a server-side proxy page, that impersonates the service on the other server, so you need to traffic stuff through the proxy.

so you need a -> b -> c and c -> b -> a to get the return. This proxy will have to be ASP or PHP.

How disparate are the domains? If the 2 servers are actually on the same LAN, you can probably run the WebService on the same machine, using an UNC in IIS for example. Alternatively, Firefox and browsers *should* be clever enough to allow different subdomains so www.domain.com hosting the front-end is on one server but backendwebservice.domain.com is pointing to the other server, allowing cross-domain transactions, yet since it's part of the same domain it should be ok.
0
 
jtmerchAuthor Commented:
In this case 2 different domains = 2 different servers for reasons beyond my control.

When you say traffic stuff through the proxy I'll still need to use Javascript to connect to that alternative server and Javascript isn't allowing me to connect to another domain from the initial domain.  Again, I'm trying to connect to another domain on another machine.  What difference will this proxy make if I cannot connect to it another domain from the initial server?  
0
 
UnexplainedWaysCommented:
"I'll still need to use Javascript to connect to that alternative server and Javascript isn't allowing me to connect to another domain from the initial domain"

Thats what B is in that little diagram, you dont use javascript.

Here's what it should look like

<html>
<script>
    AJAX("BackToMyServer.aspx");
</script>
</html>


[BackToMyServer.aspx]

  Page_Load()
  {
     //You pull out whatever u need to from the ?=&= here
     Webservice = new webservice("http://www.offsiteserver.com/webservice.asmx");
     Response.Write(Webservice.result);
  }

[/aspx]


[offsiteserver webservice.asmx]

  //Get the same ?=&= 's here
  GoSoStuffWithThem();
  return result;

[offsiteserver]


//That code is 99% of the syntax is wrong, just usign it as an example

So, the flow would be the HTML page would use ajax to send ?data="stuff" back to YOUR server.  You server (in the page load) pulls out the data="stuff" and then sends that to the offsite webservice (a way for servers to talk via the http, exactly what u want).

That webservice then pulls data="stuff" and does whatever it does, and then it returns something, which then goes back to your server page.  This page then returns the same result back to your AJAX.

So the information goes from your html page, to your server, to the webservice and back which to the user will not know the difference.

When we say "back yo your server", i dont mean a "page resresh", just a seperate server file that handles this request.

Have a crack @ it, i'd put my money on it that it will work.
0
 
devicCommented:
>> Can I even post two forms to two different domains with one submit button (Ex: have f form but have the data posted to two different servers at the time of submission).

here is an exampe:
=============================
<script language="Javascript" type="text/javascript">
function smb(obj)
{
      var frm = document.createElement("form");
      frm.target = "yahoo";
      frm.action = "http://search.yahoo.com/bin/query";
      var inp = document.createElement("input");
      inp.type = "hidden";
      inp.name = "p";
      inp.value = obj.q.value;
      frm.appendChild(inp);
      document.body.appendChild(frm);
      frm.submit();
      return true;            
}
</script>
<form action="http://www.google.com/search"  target=google onsubmit="return smb(this)">
      <input name=q value="WM 2006">
      <input type=submit>
</form>
<iframe name=google></iframe>
<iframe name=yahoo></iframe>
0
 
jtmerchAuthor Commented:
Thank's Devic,  I'll try that some time today and will definitely let you know!
0
 
jtmerchAuthor Commented:
Unexplained, I understand what you're saying

"So, the flow would be the HTML page would use ajax to send ?data="stuff" back to YOUR server"

This is what I'm already trying to do but my customer's domain is "hisdomain.com" and my server is on "myserver.com".  The html page resides on hisdomain.com, so if he uses Ajax to send anything to my server I receive permission denied.  

Here's an example of what I'm doing that's returning a permission denied error when I try to send from his domain to mydomain.  Keep in mind that this javascript will reside on the "hisdomain", and will allow his html page to send data to "myserver":

var xmlHttp;

function createXMLHttpRequest(){
      if (window.ActiveXObject)
      {
      xmlHttp = new ActiveXObject("Microsoft.XMLHTTP");
      }
      else if(window.XMLHttpRequest)
      {
      xmlHttp = new XMLHttpRequest();
      }
}

function createQueryString(){
      var userZipCode = document.getElementById("userZipCode").value;
      var queryString = "userZipCode=" + userZipCode;
      return queryString;

}

function RequestData(){
            createXMLHttpRequest();

            var url="http://www.mydomain.com?timeStamp=" + new Date().getTime();
            var queryString = createQueryString();

            xmlHttp.open("POST", url, true);
            xmlHttp.onreadystatechange = handleStateChange;
            xmlHttp.setRequestHeader("Content-Type", "application/x-www-form-urlencoded;");
            xmlHttp.send(queryString);
}
0
 
jtmerchAuthor Commented:
Thanks Devic, it looks like this is the closest thing to what I'll need to be able to do.
0
 
devicCommented:
You are welcome.
0

Featured Post

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

  • 8
  • 3
  • 3
  • +2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now