Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>So you want to do an AJAX request from your client code to the server, recieve a response and process it in some way? You will not need sockets. Instead, use Tapestry's built-in AJAX functionality.</p> <p>If you're loading additional content inside your page via Javascript, chances are you will not need to write any code at all. Be sure you have read the <a href="http://tapestry.apache.org/tapestry5.1/guide/ajax.html" rel="nofollow noreferrer">AJAX section from the Tapestry docs</a>, and you understand what a Zone is and how it works.</p> <p>Here's a basic example. Template:</p> <pre><code>&lt;div id="myZone" t:type="Zone" t:id="myZone"&gt; ... [Initial content, if any] ... &lt;/div&gt; &lt;a t:type="ActionLink" t:id="updateContent" t:zone="myZone"&gt;Update&lt;/a&gt; </code></pre> <p>And class:</p> <pre><code>@Inject private Zone myZone; @Inject private Request request; @OnEvent(component = "updateContent") Object updateContent() { ... [your code] .... if (this.request.isXHR()) { return this.myZone.getBody(); } else { return this; } } </code></pre> <p>Tapestry will do everything else, like registering the proper event listener on the link and inserting the updated content in the proper place. The <code>if (this.request.isXHR())</code> makes sure your page will <a href="http://en.wikipedia.org/wiki/Unobtrusive_JavaScript" rel="nofollow noreferrer">degrade gracefully</a> for clients without JavaScript enabled.</p> <p>If you'd like to do something else entirely, like returning a JSON object and processing it with your own JavaScript code, you can return any of <a href="http://tapestry.apache.org/tapestry5.1/apidocs/org/apache/tapestry5/json/package-summary.html" rel="nofollow noreferrer">these JSON classes</a> from your event handler.</p> <p>Also, if you want to write your own client-side code, be sure to use the built-in, cross-browser <a href="http://www.prototypejs.org/api/ajax" rel="nofollow noreferrer">AJAX functionality of Prototype</a>, which ships with Tapestry.</p> <p><em>Edit based on comment:</em></p> <p>You won't be able to access a different server (host + port) through AJAX because of the <a href="http://en.wikipedia.org/wiki/Same_origin_policy" rel="nofollow noreferrer">same origin policy</a>. You could, however, proxy the call through your Tapestry app. I've modified my code to illustrate this (assuming the thing listening on port 2112 is an HTTP server, otherwise change as needed):</p> <pre><code>@OnEvent(component = "updateContent") Object updateContent() throws IOException { final URL url = new URL("http://localhost:2112"); final HttpURLConnection con = url.openConnection(); final String content; InputSteam input = null; try { input = con.getInputStream(); content = IOUtils.toString(input); } finally { IOUtils.closeQuietly(input); } return new StreamResponse() { @Override public String getContentType() { return "text/javascript"; } @Override public InputStream getStream() throws IOException { return new ByteArrayInputStream(content.getBytes("UTF-8")); } @Override public void prepareResponse(Response response) { response.setHeader("Expires", "0"); response.setHeader("Cache-Control", "must-revalidate, post-check=0, pre-check=0"); } } } </code></pre>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload