Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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?

like image 659
Steve Horn Avatar asked Jun 26 '26 17:06

Steve Horn


2 Answers

Generally speaking, the proxy runs on your web server - most likely IIS in your case - and 'relays' the requests to another server on a different domain.

Here's an example of one implemented in C# .NET

Fast, Streaming AJAX proxy

like image 72
neonski Avatar answered Jun 28 '26 06:06

neonski


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("http://www.someothersite.com/webservice?callback=?", function(result)
{
    doStuffWithResult(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.

like image 24
Matt Ephraim Avatar answered Jun 28 '26 06:06

Matt Ephraim



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!