1

Ho can i bypass this restriction? I know i can use some kind of a proxy but not sure how this proxy should look like?

Any other suggestions?

Erik Sapir
  • 23,209
  • 28
  • 81
  • 141
  • possible duplicate of [Cross-site XMLHttpRequest](http://stackoverflow.com/questions/395045/cross-site-xmlhttprequest) – Hamish Jan 10 '14 at 06:09

4 Answers4

2

Here is a pretty straightforward tutorial: http://developer.yahoo.com/javascript/howto-proxy.html

Basically, you make a service that takes an xmlhttprequest and have to request data from the external domain, and then return the result.

Andrew Jackman
  • 13,781
  • 7
  • 35
  • 44
1

JSONP is exactly for that purpose

JSONP or "JSON with padding" is a complement to the base JSON data format, a pattern of usage that allows a page to request data from a server in a different domain. As a solution to this problem, JSONP is an alternative to a more recent method called Cross-Origin Resource Sharing.

Here is the very very basic example of JSONP implementation.

The server side code -

 public string GetFirstName()
        {
            string callback = Request.QueryString["callback"];
            string resultJson = "{\"FirstName\": \"archil\"}"; //should definitely be some more application specific data :)

            if (!string.IsNullOrEmpty(callback))
            {
                return string.Format("{0}({1})", callback, resultJson);
            }
            return resultJson;
        }

This method is mapped to /GetFirstname URL on server. It is extacting callback argument from query string. And wrapping generated resultJson as javascript function call where name of function is parameter passed with callback.

on the client side, using jQuery - implementation is as simple as

$(function () {

        $.ajax('http://serverUrl/GetFirstName', {

            dataType: 'JSONP',

            jsonpCallback: 'alert'

        });

    });

This will pass function name alert as callback for server. Server will return alert({"FirstName": "archil"}). jQuery will automatically inspect this response and execute it. As the result, you will get standart alert screen in browser. Main idea is that alert is executed will the server's return parameters. You could pass more specific function name as jsonpCallback and act on results of request.

I KNOW that the URL pattern used here is more like RPC style web service than REST style, but the example is about using JSONP, not about REST architecture

archil
  • 39,013
  • 7
  • 65
  • 82
  • I am not sure how i can use JSONP to perform REST requests – Erik Sapir May 16 '11 at 11:35
  • By the way, you cannot perform REST requests, the requests you mean are HTTP requests. JSONP should be supported by server. Read the wikipedia article, there's full explanation about it - http://en.wikipedia.org/wiki/JSONP#How_it_works – archil May 16 '11 at 11:41
  • I would appreciate an example of how to perform a REST/REQUEST with response. BTW - what do you mean by 'JSONP should be supported by server'? – Erik Sapir May 16 '11 at 12:02
  • does that mean that the server (Webdav server) must be aware of JSON? – Erik Sapir May 16 '11 at 12:41
  • Yes, server should generate some JSON but return it wrapped inside callback call – archil May 16 '11 at 13:03
  • i can't change the server. Also, it is a java server, not js – Erik Sapir May 16 '11 at 13:59
  • I'm not sure how to implement or is it possible to implement this without server's support. But for sure, any modern web service can return JSON, it does not have to be javascript server. At last, JSON is just formatted string, nothing more – archil May 16 '11 at 18:51
0

You can use $.ajax() call.

This has property crossdomain: which handles the cross domain request.

http://api.jquery.com/jQuery.ajax/

For cross domain request using jquery have a look at the http://net.tutsplus.com/tutorials/javascript-ajax/quick-tip-cross-domain-ajax-request-with-yql-and-jquery/

dhinesh
  • 4,676
  • 2
  • 26
  • 44
  • can you add code example - how i can use it instead of xmlHttpRequest (for example for DELETE request, not POST or GET request). – Erik Sapir May 16 '11 at 11:26
  • will this property actually replace the need for a proxy, to do an ajax request on google.ca, for example? – Andrew Jackman May 16 '11 at 11:27
  • This is not correct as CORS only works in some new browsers, and on servers that are correctly set up with headers. – Sean Kinsey May 16 '11 at 12:54
0

Here are basic steps to create such proxy.

  • Create server side page (using server side language like PHP or ASP.NET it doesn't really matter) and call it for example "MyProxy.aspx"
  • In the server side code read the data sent either on URL or as POST data, and send that data as Web Request (in case of .NET, there's surely equivalent in PHP and other languages) to the external website.
  • Parse the result sent from the external website and send it back to the client.
  • In the client, send AJAX request to the proxy page (e.g. MyProxy.aspx) passing it the proper data, and handle the response.

Let us know what server side language you're using for more technical help to implement the above.

Shadow The GPT Wizard
  • 66,030
  • 26
  • 140
  • 208