JSONP Proxy

This is a tool that you can use to turn a JSON request into a JSONP request.

The reason for this is that you can't make a reuqest from Javascript to a different domain unless you use JSONP, and to do that the web service has to actually return a JSONP response. With this tool you can make a JSONP request to a web service that returns JSON, and get it converted to JSONP.

Usage

You use the page http://jsonp.guffa.com/Proxy.ashx to make the request. Put the URL of the web service that you want to call in the url parameter.

Here is an example using jQuery to call a web service:

	$.get(
	  "http://jsonp.guffa.com/Proxy.ashx?url=weather.yahooapis.com%2fforecastjson%3fw=2502265",
	  function(data) {
	    alert(data.location.city + ': ' + data.condition.text);
	  },
	  "jsonp"
	);
			

Notes

There are some characters that you may need to encode when you put the URL in a parameter. Write slash (/) as %2f and ampersand (&) as %3f. If you have other characters that you wonder about, you can read about URL encoding.

What it does

Basically the proxy only requests the data from the web service, wraps it in a Javascript call, and returns it with a different content type. If the web service for example returns {"status":"ok"}, the proxy will return callback({"status":"ok"});.

What it doesn't do

The proxy doesn't validate the result from the web service in any way, it only wraps it and returns it. If the web service returns garbage, the proxy will return garbage wrappped in a Javascript call.

Additional features

The "http://" protocol part is optional for the URL in the url parameter.

You can specify a callback parameter that will be used as the function name in the JSONP response. (jQuery adds this parameter automatically when you specify the data type "jsonp".) If you don't specify a callback parameter, the default is "callback".

You can specify a type parameter to force the content type of the response. Example: &type=text%2fplain If you don't specify a type parameter, the default is "application/javascript".

If the request to the web service fails, the proxy returns a JSONP result with the status, URL and error message, in the form callback({"status":"error","url":"http://some.com/service","message":"The error message"});.

Questions

If you have any questions about this tool, you can mail guffa@guffa.com.