Returning json when request is non xhr

classic Classic list List threaded Threaded
3 messages Options
Reply | Threaded
Open this post in threaded view
|

Returning json when request is non xhr

nquirynen
Hi,

I have 2 applications (different servers):
1) html + js
2) Tapestry5

Now I tried to do an ajax request (jquery) from application 1 to
application 2 where I want to return some json back to application 1.
But because cross domain get requests do not use XHR I get following
error in application 2:

Return type org.apache.tapestry5.json.JSONObject can not be handled.

So how can I return json if the request is not XHR ?
Reply | Threaded
Open this post in threaded view
|

Re: Returning json when request is non xhr

Chris Poulsen
You could try to add your own ComponentEventResultProcessor<JSONObject> -
However I do not know whether this interferes with the built in tapestry
stuff for ajax - If that is the case you could probably return a different
type of json.

Check (StreamResponse/StreamResponseResultProcessor) in tapestry sources
for an example of how to add the code

--
Chris

On Mon, Dec 15, 2014 at 4:16 PM, Nathan Quirynen <
[hidden email]> wrote:

>
> Hi,
>
> I have 2 applications (different servers):
> 1) html + js
> 2) Tapestry5
>
> Now I tried to do an ajax request (jquery) from application 1 to
> application 2 where I want to return some json back to application 1.
> But because cross domain get requests do not use XHR I get following
> error in application 2:
>
> Return type org.apache.tapestry5.json.JSONObject can not be handled.
>
> So how can I return json if the request is not XHR ?
>
Reply | Threaded
Open this post in threaded view
|

Re: Returning json when request is non xhr

Thiago H de Paula Figueiredo
In reply to this post by nquirynen
On Mon, 15 Dec 2014 13:16:48 -0200, Nathan Quirynen  
<[hidden email]> wrote:

> Hi,

Hi!

>
> I have 2 applications (different servers):
> 1) html + js
> 2) Tapestry5
>
> Now I tried to do an ajax request (jquery) from application 1 to
> application 2 where I want to return some json back to application 1.
> But because cross domain get requests do not use XHR I get following
> error in application 2:
>
> Return type org.apache.tapestry5.json.JSONObject can not be handled.
>
> So how can I return json if the request is not XHR ?

Just wrap the JsonObject or JsonArray in a StreamResponse when the request  
isn't AJAX:

if (request.isXHR()) {
        return jsonObject;
else {
        return new TextStreamResponse("application/json", jsonObject.toString());
}

--
Thiago H. de Paula Figueiredo
Tapestry, Java and Hibernate consultant and developer
http://machina.com.br

---------------------------------------------------------------------
To unsubscribe, e-mail: [hidden email]
For additional commands, e-mail: [hidden email]