Link to home
Start Free TrialLog in
Avatar of cilerler
cilerler

asked on

jQuery JSONP WCF/ASMX Cross Domain

I spent almost 40 hours to find a single working article but couldn't.  So please be kind and don't just give me a link.  :)  Again, I spend serious time on this and probably i saw and tried every link you can find in Google.

I have a web site which is in US server and web service which is in UK server with IIS7+WS2008

Doesn't matter if solution is ASMX or WCF based as long as there is a solution!

All I want to do is making a simple "HelloWorld" call via simple HTML page with jQuery from US  server to UK server.

Please feel free to reply if you know something about it.

Thank you very much for your time and attention.

Avatar of Steve Krile
Steve Krile
Flag of United States of America image

I am going to guess that you want information on one server (UK) to be "fed" to information on another server (US) using some sort of AJAX technology (jQuery).  If my assumptions are correct, the short answer is you cannot do this directly.  

So, you either need to write code on your US server that queries the UK server (this is allowed on the server-side), or add another library to your mix : http://www.ajax-cross-domain.com/

Avatar of leakim971
example here work for me fine : http://www.simple-talk.com/dotnet/asp.net/calling-cross-domain-web-services-in-ajax

The proxy which really do the call to the web service : http://www.webservicex.net/CurrencyConvertor.asmx

Save this file as : callservice.aspx




<%@ Import Namespace="System.Data" %>
<%@ Import Namespace="System.Xml" %>
<%@ Page Language="C#" Debug="true" %>
<%
	XmlDocument wsResponse = new XmlDocument();
        string url =  "http://www.webservicex.net/CurrencyConvertor.asmx/ConversionRate?FromCurrency=" + Request.QueryString["FromBox"].ToString() + "&ToCurrency=" + Request.QueryString["ToBox"].ToString();
        wsResponse.Load(url);
        string XMLDocument = wsResponse.InnerXml;
        Response.ContentType = "text/xml";
        Response.Write(XMLDocument);
%>

Open in new window

The main page : Default.htm


<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="Default.aspx.cs" Inherits="WebApplication16._Default" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml" >
<head runat="server">
<title>test</title>
<script language="javascript" type="text/javascript">
    var xmlhttprequest = null;

    function initiateConversion() {
        xmlhttprequest = createRequestObject();
        var url = "callservice.aspx?FromBox="+ document.getElementById("FromBox").value+ "&ToBox=" + document.getElementById("ToBox").value;
        xmlhttprequest.open("GET", url, true);
        xmlhttprequest.onreadystatechange = getData;
        xmlhttprequest.send(null);
    }

function createRequestObject() 
{
        if (window.XMLHttpRequest) 
        {
                return xmlhttprequest = new XMLHttpRequest(); 
        } 
      else if (window.ActiveXObject) 
      {  
            return xmlhttprequest = new ActiveXObject("Microsoft.XMLHTTP"); 
      } 
}
function getData() 
{
  if ((xmlhttprequest.readyState == 4) &&( xmlhttprequest.status == 200))
  {
    var myXml = xmlhttprequest.responseXML;
    var xmlobject = null;
    var XMLText = null;
    if (window.ActiveXObject)
    {
        XMLText = myXml.childNodes[1].firstChild.nodeValue; 
    }
    else
    {
        XMLText = myXml.childNodes[0].firstChild.nodeValue;
    }
    
    var table = document.getElementById("table1");
    var row = table.insertRow(table.rows.length);
    var tablecell = row.insertCell(row.cells.length);
    tablecell.appendChild(document.createTextNode(document.getElementById("FromBox").value + " to " + document.getElementById("ToBox").value));
    var tablecell = row.insertCell(row.cells.length);
    tablecell.appendChild(document.createTextNode(XMLText));
    table.setAttribute("border", "2");
  } 
}
</script>
</head>
<body>
Currency To Convert From:
<select id="FromBox">
<option value="USD" selected="selected">USD - U.S. Dollar</option>
<option value="GBP">GBP - British Pound</option>
<option value="EUR">EUR - Euro</option>
<option value="JPY">JPY - Japanese Yen</option>
</select>
Currency To Convert To:
<select id="ToBox">
<option value="USD">USD - U.S. Dollar</option>
<option value="GBP" selected="selected">GBP - British Pound</option>
<option value="EUR">EUR - Euro</option>
<option value="JPY">JPY - Japanese Yen</option>
</select>
<br /><br />
<input id="button1" type="button" value="Click to convert currency" onclick="initiateConversion()" />
<br /><br />
<table id="table1">
</table>
</body>
</html>

Open in new window

Put the two file in same web folder.
Tested on MS Windows Server 2008 with Chrome, IE, Safari, Opera and Firefox
ASKER CERTIFIED SOLUTION
Avatar of cilerler
cilerler

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
I was clearly in over my head with this question.  But, I learned something!  Thanks for posting that .net 4 link.  JSONP sounds a bit scary and I'm not convinced there aren't more serious cross-site security issues, but it does solve the problem of getting data from out there into here.