Hi Adam
I've finally worked it out and a lot of it is very much down to your
help!!
What I wasn't reading on Dan's article was a very important part of
his Python server, basically the line:
body = '%s(%s);' % (fun_name, file('json.js').read())
which as you quite rightly stated includes the damn function name and
is very important!!!
So I have taken your advice and implemented my client side code as per
Dans article:
============
public void makeCall(String number)
{
String serverURL = ClientGlobals.CALL_SERVICE_URL+"callback=";
String callbackName = reserveCallback();
setup(this, callbackName);
System.out.println(serverURL + callbackName);
System.out.println(callbackName);
addScript(callbackName, serverURL + callbackName);
}
public String reserveCallback()
{
while (true)
{
if (!callbacks.containsKey(new Integer(curIndex)))
{
callbacks.put(new Integer(curIndex), null);
return "__gwt_callback" + curIndex++;
}
}
}
/**
* <p>Adds the JSONP script to our widget so we can make XSS requests
baby</p>
*
* @param uniqueId The unique id of the call
* @param url The URL of our Request
*/
public void addScript(String uniqueId, String url)
{
Element e = DOM.createElement("script");
DOM.setElementAttribute(e, "language", "JavaScript");
DOM.setElementAttribute(e, "src", url);
scriptTags.put(uniqueId, e);
DOM.appendChild(RootPanel.get().getElement(), e);
}
public void handle(JavaScriptObject jso)
{
if( jso != null )
{
Window.alert("Woohoo JSO is not null it bloody worked");
}
}
/**
*
* <p>Sets up our Javascript cross site JSON call</p>
*
* @param model Handles our Cross Site JSON call
* @param callback
*/
public native static void setup(CallModel model, String callback)
/*-{
$wnd[callback] = function(someData)
{
model.@com.myPackage.MyCall::handle(Lcom/google/gwt/core/client/
JavaScriptObject;)(someData);
}
}-*/;
============
Then importantly in my server side I have used the callback name in my
JSON output as you quite rightly mention!! :)
protected void doPost(HttpServletRequest req, HttpServletResponse
resp) throws ServletException, IOException
{
String asyncCallback = req.getParameter("callback");
String output = asyncCallback+"([{color: \"red\",value: \"#f00\"}])";
resp.setContentType("text/javascript");
resp.addHeader("Pragma", "no-cache");
resp.setStatus(200);
PrintWriter out = resp.getWriter();
out.println(output);
}
And its not hitting my handler
Thank you very much for your help. I'm going to post a tutorial on my
blog
http://eggsylife.blogspot.com and will credit help toward
yourself.
Eggsy