Monday, March 26, 2012

Re: Can't pass data using JsonpRequestBuilder

Thanks.  That was a big help.  Ultimately, I am trying to pass XML.  The following worked for me:

Client:

public void onClick(ClickEvent event) {

JsonpRequestBuilder jsonprb= new JsonpRequestBuilder();

jsonprb.requestString("http://localhost:8084/Arahant/GWTServlet"

new AsyncCallback<String>() {

                @Override

                public void onFailure(Throwable caught) {

                    Window.alert("Failure getting JSONP directly from remote server");

                }


@Override

public void onSuccess(String result) {

Window.alert("Got " + result);

}

            });


Server:


protected void processRequest(HttpServletRequest request, HttpServletResponse response)

throws ServletException, IOException {

String callback = request.getParameter("callback");

PrintWriter out = response.getWriter();

try {

out.print(callback + "(\"Hello there\")");

} finally {

out.close();

}

}


I figure I'll stick XML in the string.



On Mon, Mar 26, 2012 at 3:54 AM, Thomas Broyer <t.broyer@gmail.com> wrote:
Your server sends an array with a single item being an element with a "Hello" property, so your client code should use an AsyncCallback<JsArray<X>> where X is some JavaScriptObject subclass with a JSNI method getting the "Hello" property

class X extends JavaScriptObject {
    protected X() { /* required by JSO */ }

    public final native String getHello() /*-{ return this.Hello; }-*/;
}

...

jsonprb.requestObject(url, new AsyncCallback<JsArray<X>>() {
    @Override
    public void onSuccess(JsArray<X> result) {
       X firstX = result.get(0);
       Window.alert("Hello " + firstX.getHello());
    }
...
}

On Monday, March 26, 2012 4:05:42 AM UTC+2, Blake wrote:
Greetings,

I have a GWT app talking to a Java servlet just fine except I can't seem to pass data.  I've already spent a day on it.  Any help would be greatly appreciated.

I am using JsonpRequestBuilder and attempting to get around SOP.  Here is my server code:

public class GWTServlet extends HttpServlet {
protected void processRequest(​HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
String callback = request.getParameter("​callback");
PrintWriter out = response.getWriter();
try {
out.print(callback + "([{\"Hello\": \"world\"}]);");
} finally {
out.close();
}
}....

Here is my GWT code:

public void onClick(ClickEvent event) {

JsonpRequestBuilder jsonprb= new JsonpRequestBuilder();

jsonprb.requestObject("http://​localhost:8084/XXX/GWTServlet"​, 

new AsyncCallback() {

              @Override

              public void onFailure(Throwable caught) {

                  Window.alert("Failure getting JSONP directly from remote server");

              }


@Override

public void onSuccess(Object result) {

Window.alert("Got response");

JavaScriptObject res = (JavaScriptObject) result;

Window.alert(res.toSource());

}

          });



I get the "Got response" message.  I am also able to put break points on the frontend or backend and it gets to where I expect.  The problem is that I can't seem to get at the data send from the server.  I don't know what to do in onSuccess().

Of course, once I get this working I will have the same challenge going the other way.  Help is greatly appreciated.

Blake McBride


--
You received this message because you are subscribed to the Google Groups "Google Web Toolkit" group.
To view this discussion on the web visit https://groups.google.com/d/msg/google-web-toolkit/-/-r5aW6rcfGMJ.
To post to this group, send email to google-web-toolkit@googlegroups.com.
To unsubscribe from this group, send email to google-web-toolkit+unsubscribe@googlegroups.com.
For more options, visit this group at http://groups.google.com/group/google-web-toolkit?hl=en.

--
You received this message because you are subscribed to the Google Groups "Google Web Toolkit" group.
To post to this group, send email to google-web-toolkit@googlegroups.com.
To unsubscribe from this group, send email to google-web-toolkit+unsubscribe@googlegroups.com.
For more options, visit this group at http://groups.google.com/group/google-web-toolkit?hl=en.

No comments:

Post a Comment