Jersey + Jsonp + jQuery

At work we are developing an application that required me to create a webservice that could be called from another domain with javascript. Hopefully this will help you do just that. I am using Jersey 1.4 and jQuery 1.4.4.

Here is my Jersey java code for the webservice:

package com.test;

import com.sun.jersey.api.json.JSONWithPadding;

public class Test {
	public JSONWithPadding testJSONP(@QueryParam("jsoncallback") String callback{
		String retVal="YES";
		return new JSONWithPadding(
                              new GenericEntity("{"test":"" + retVal + ""}"){},callback);

You can see what gets returned by pasting in the browser

It should return: callback({“test”:”YES”})

Now to make this call in jQuery:

    url: '', //Sub the to the ip or name of where the webservice is located.
    type: 'GET',
    dataType: 'jsonp',
    jsonp: 'jsoncallback',
    success: function(data){

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.