Steve Horn Steve Horn - 1 year ago 97
ASP.NET (C#) Question

Cross browser scripting proxy

I am developing some client side Javascript that is using some JSON web services on a different domain. I have read that some browsers do not allow cross-domain scripting and that I should create a proxy on my local server to serve the data.

Can someone please point me to a simple example of how to do this in ASP.Net?

Answer Source

You may be able to avoid a proxy by using a technique like JSONP. Assuming the web service you're talking to supports JSONP (for example, Flickr or Twitter both offer a JSONP API) or you have control over the data the web service sends back, you can send JSON data between domains using a library that features JSONP.

For example, in jQuery, you can make a remote JSON call:

jQuery.getJSON("", function(result)

Because the call is to another domain, jQuery automatically uses some trickery to make a cross domain call. jQuery will automatically replace the ? in the url with a callback function name that the web service can use to format the JSON data being returned.

If you're the one controlling the web service, you can handle the JSONP request by getting the request parameter called "callback" which will be set to the callback function name you need to use. The callback function takes one parameter, which is the JSON data you want to send back. So, if the callback parameter is set to "jsonp2342342", you'll want the web service to respond like this:

jsonp2342342({key: value, key2: value});

If the web service you're using already supports JSONP, you won't have to worry about doing the formatting yourself.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download