How to use RESTful urls with JSONp including jQuery example
Following up on the previous post from Making cross domain JSON work with jQuery and RESTful urls, I was left with a solution that wouldn’t work for me. After a little more searching I found a JSONp library on google code that allows the user to specify a request url explicitly without any kind of automatic url generation. To me, this implied the capacity to support REST and fortunately it does.
I’ll start with the syntax for the call using the jsonp plugin (note I’m not using jsonp to construct the url):
url = "http://otherdomain.com/getMyFavoriteImage";
url += "/tag_marine";
url += "/format_json";
url += "/callback_myFunction";
$.jsonp({
"url" : url,
callback : "myFunction",
cache : "false",
success:function(data){
$("<img alt="" />").attr("src", data.imageurl).appendTo("#images");
}
});
After modifying my web service, I was able to receive the following response based on this request:
myFunction({
imageurl: 'http://test.com/focktopus.jpg',
description: "1 legged octopus"
});
You can read the rest of JSONp’s API on their google code page, but there are two parameters that were critical for me.
- callback – This is the name of the callback function that will be expected from the web-service response. It can be called anything (_jqjsp is the default callback name)
- cache – I needed to set this to false because I didn’t want a cache buster appended to the end of my urls. This is set to true by default and would mess up my edge cacheing (I want to manage that from the cms)
How the callback works
I wanted to assemble the url myself based on my variable structure (which JSONp allows you to do since the url is just an unmodified argument). If you’ll notice I’m passing in a callback name of “myFunction” in my url by passing in the argument callback_myFunction. The “callback_” designates this variable as the name of the function to wrap the response in to my web-service. Some web-services that support JSONp do not allow you to customize the name of the callback function in their response. In these cases you would just specify the expected function as the callback parameter.
I’ll follow up with a post soon on the webservice itself.

When the control bar is initialized, the padding is added to the left cursor and the right cursor is set to the width of the control bar. When a component is added to the control bar calling addFromLeft, the following happens: