How rendering in views by using JSON?

168 views
Skip to first unread message

eson

unread,
May 23, 2012, 9:14:53 AM5/23/12
to play-fr...@googlegroups.com
Hi,
   How I will render my Json values in views.scala.html in play 2.0.

Application:

public static Result ajaxjson()    {
      String contentType = "text/json";
      response().setContentType(contentType);

\\  if am setting this cotent type (above tow line) means new download window open ajaxjson.json file format
\\  Is it correct to use XXX.scala.html file   using for routes/XXX.json?

      ObjectNode error = Json.newObject();      
      error.put("error", "ERROR");                        \\                
      return ok(ajaxjson.render(error));
}

Routes:

GET    /ajaxjson.json    controllers.Application.ajaxjson()

views:

Ajaxjson.scala.html

@(error:org.codehaus.jackson.node.ObjectNode)
@error

THanks,

Eson.


Nilanjan

unread,
May 23, 2012, 9:29:02 AM5/23/12
to play-framework
Have you tried:

String contentType = "text/html";

This will show the json error message as text as part of the view.

Nilanjan

biesior

unread,
May 23, 2012, 9:35:07 AM5/23/12
to play-fr...@googlegroups.com
You don't need the separate view and also don't need to specify the contentType:

    public static Result ajaxjson()    {

        ObjectNode error = Json.newObject();
        error.put("error", "ERROR");
        return ok(error);
    }

route is ok:
GET    /ajaxjson.json    controllers.Application.ajaxjson()

biesior

unread,
May 23, 2012, 9:40:27 AM5/23/12
to play-fr...@googlegroups.com
Hahaha, de facto if it returns some error it should be badRequest, of course from technical point of view it doesn't differ but you can use status code in ajax for an  example to perform other action on errors.

        return badRequest(error);

techpost it

unread,
May 23, 2012, 9:55:39 AM5/23/12
to play-fr...@googlegroups.com

Hi,

   Thanks lot Nilanjan and Biesior,

   

     I am just done for programmatic convention not according to error rendering (not for HTTP code). Ok I changed Json key value.


    now am received  {"myerror":"OWN ERROR"}  in ajaxjson.json file.


    i want to send this value into Application.readJson() method how i will do..?



--
You received this message because you are subscribed to the Google Groups "play-framework" group.
To view this discussion on the web visit https://groups.google.com/d/msg/play-framework/-/04E3pwK8HjIJ.

To post to this group, send email to play-fr...@googlegroups.com.
To unsubscribe from this group, send email to play-framewor...@googlegroups.com.
For more options, visit this group at http://groups.google.com/group/play-framework?hl=en.



--
Thanks,
  Eson.

biesior

unread,
May 23, 2012, 3:16:51 PM5/23/12
to play-fr...@googlegroups.com
But specify. do you want to get it in the view ie. via ajax and then send to other controller also with ajax, or just need to send the json to some method if error occures from ajaxjson() ?


W dniu środa, 23 maja 2012 15:55:39 UTC+2 użytkownik eson napisał:

Hi,

   Thanks lot Nilanjan and Biesior,

Reply all
Reply to author
Forward
0 new messages