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.

Who is Participating?
cilerlerAuthor Commented:
@skrile: thx for your time but not the answer I was looking for.
@leakin971: big thank you for your time and attention.  It should work but it is kind of a workaround.  Which is not working for me.  And the article was great thx for that.

If anyone wants to learn how to handle this with .Net4 please check http://goo.gl/SPw1

Thanks again.

Steve KrileCommented:
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/

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();
        string XMLDocument = wsResponse.InnerXml;
        Response.ContentType = "text/xml";

Open in new window

Cloud Class® Course: CompTIA Healthcare IT Tech

This course will help prep you to earn the CompTIA Healthcare IT Technician certification showing that you have the knowledge and skills needed to succeed in installing, managing, and troubleshooting IT systems in medical and clinical settings.

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">
<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;

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; 
        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);
    table.setAttribute("border", "2");
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>
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>
<br /><br />
<input id="button1" type="button" value="Click to convert currency" onclick="initiateConversion()" />
<br /><br />
<table id="table1">

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
Steve KrileCommented:
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.
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.