Returning JSONP

3 messages Options
Embed this post
Permalink
Nathan Wallman

Returning JSONP

Reply Threaded More More options
Print post
Permalink
I'm trying to use JQuery to query my ServerResource and then process a returned JSONP object. Everything I'm returning appears to be correct but when JQuery tries to process it nothing happens. As far as I can tell my resource returns everything that other REST services do but for some reason mine doesn't work.  Here are the details about my setup:

Here is the JQuery I'm using:

http://docs.jquery.com/Ajax/jQuery.getJSON

I am running it from the Google AJAX Playground found here:

http://code.google.com/apis/ajax/playground/#jquery

Here is how I'm using JQuery:

google.load("jquery", "1");

// on page load complete, fire off a jQuery json-p query
// against Google web search
function OnLoad(){
  var url = "http://teamwebtools.appspot.com/api/hello?callback=?";
  $.getJSON(url, function () {
   alert("test");
  });
}

google.setOnLoadCallback(OnLoad);

Here is the URL that is mapped to my ServerResource:

http://teamwebtools.appspot.com/api/hello

Finally here is the code of my ServerResource:

        @Get
        public Representation getHelloWorld() throws Exception{
                log.log(Level.WARNING,"Incoming Request");
                StringRepresentation rep = new StringRepresentation("({\"responseData\":\"hello, world (from the cloud!)\"})", MediaType.APPLICATION_JAVASCRIPT);
                return rep;
        }

Now if you try that URL you will see that my Resource is responding with data but I can't figure out why JQuery isn't processing it. You compare my data with another REST Api and you'll see they are very similar:

http://api.flickr.com/services/feeds/photos_public.gne?tags=cat&tagmode=any&format=json&jsoncallback=?

I want to use this particular JQuery tool because I need to be able to hit my REST service from multiple domains.  If someone could help me get over this hurdle I would greatly appreciate it.

Thanks very much!

Nathan

------------------------------------------------------
http://restlet.tigris.org/ds/viewMessage.do?dsForumId=7458&dsMessageId=2374656
Nathan Wallman

RE: Returning JSONP

Reply Threaded More More options
Print post
Permalink
I think I might have found a solution to this problem.  I think my problem is I'm not returning the proper callback method wrapping the JSON.  I found some example source here:

http://www.ibm.com/developerworks/library/wa-aj-jsonp1/?ca=dgr-jw64JSONP-jQuery&S_TACT=105AGY46&S_CMP=grsitejw64

Which looks like this:

@Override
protected void doGet(HttpServletRequest req, HttpServletResponse resp)
  throws ServletException, IOException {
        String jsonData = getDataAsJson(req.getParameter("symbol"));
        String output = req.getParameter("callback") + "(" + jsonData + ");";

        resp.setContentType("text/javascript");
         
        PrintWriter out = resp.getWriter();
        out.println(output);
        // prints: jsonp1232617941775({"symbol" : "IBM", "price" : "91.42"});
}


So now my next question would be how is the best way to grab request parameters?

So would I do something like this:

/api/hello?{params}

And then parse them all out manually or is there a built in feature that I can leave my URI as the orginal (/api/hello) and get out the request parameters?

Thanks,

Nate

------------------------------------------------------
http://restlet.tigris.org/ds/viewMessage.do?dsForumId=7458&dsMessageId=2374851
Rhett Sutphin

Re: Returning JSONP

Reply Threaded More More options
Print post
Permalink
In reply to this post by Nathan Wallman
Hi Nathan,

I'm not a JSONP expect, but I have a suggestion (below).

On Jul 22, 2009, at 11:27 PM, Nathan Wallman wrote:

> I'm trying to use JQuery to query my ServerResource and then process  
> a returned JSONP object. Everything I'm returning appears to be  
> correct but when JQuery tries to process it nothing happens. As far  
> as I can tell my resource returns everything that other REST  
> services do but for some reason mine doesn't work.  Here are the  
> details about my setup:
>
> Here is the JQuery I'm using:
>
> http://docs.jquery.com/Ajax/jQuery.getJSON
>
> I am running it from the Google AJAX Playground found here:
>
> http://code.google.com/apis/ajax/playground/#jquery
>
> Here is how I'm using JQuery:
>
> google.load("jquery", "1");
>
> // on page load complete, fire off a jQuery json-p query
> // against Google web search
> function OnLoad(){
>  var url = "http://teamwebtools.appspot.com/api/hello?callback=?";
>  $.getJSON(url, function () {
>   alert("test");
>  });
> }
>
> google.setOnLoadCallback(OnLoad);
>
> Here is the URL that is mapped to my ServerResource:
>
> http://teamwebtools.appspot.com/api/hello
>
> Finally here is the code of my ServerResource:
>
> @Get
> public Representation getHelloWorld() throws Exception{
> log.log(Level.WARNING,"Incoming Request");
> StringRepresentation rep = new  
> StringRepresentation("({\"responseData\":\"hello, world (from the  
> cloud!)\"})", MediaType.APPLICATION_JAVASCRIPT);
> return rep;
> }

The idea with JSONP is that the response from the server is wrapped in  
a function call so that when the result is evaluated in the page, the  
callback function is automatically invoked with the data.  In your  
flickr example, e.g., the default callback is "jsonFlickrFeed".  You  
can change it by providing a different name to the jsoncallback  
parameter:

http://api.flickr.com/services/feeds/photos_public.gne?tags=cat&tagmode=any&format=json&jsoncallback=somethingElse

Your resource needs to act the same way.  As it is, you are only  
wrapping the data in parens.

Rhett

> Now if you try that URL you will see that my Resource is responding  
> with data but I can't figure out why JQuery isn't processing it. You  
> compare my data with another REST Api and you'll see they are very  
> similar:
>
> http://api.flickr.com/services/feeds/photos_public.gne?tags=cat&tagmode=any&format=json&jsoncallback= 
> ?
>
> I want to use this particular JQuery tool because I need to be able  
> to hit my REST service from multiple domains.  If someone could help  
> me get over this hurdle I would greatly appreciate it.
>
> Thanks very much!
>
> Nathan
>
> ------------------------------------------------------
> http://restlet.tigris.org/ds/viewMessage.do?dsForumId=7458&dsMessageId=2374656

------------------------------------------------------
http://restlet.tigris.org/ds/viewMessage.do?dsForumId=7458&dsMessageId=2374903