Problem Rendering JSON

32 views
Skip to first unread message

rest bowling

unread,
May 9, 2012, 12:16:13 AM5/9/12
to bowler...@googlegroups.com
Apologize for the noob question... Trying to render JSON and receving a null response. Code proves that the GET is being received from sbt console and that data is not null. Any help would be greatly appreciated ... Using irebug the response is empty and the alert prints "JSON View =null"

My Controller

package bowlerquickstart

import org.bowlerframework.controller.{Controller,FunctionNameConventionRoutes}
import org.bowlerframework.model.{ ParameterMapper, Validations}
import org.bowlerframework.view.{Renderable}
import org.bowlerframework._

/**
 *
 * extends:
 * - Controller: used to construct routes and deal with them by providing functions that respond to routes.
 * - ParameterMapper: takes a request and maps any values into beans or other objects.
 * - Validations: validation enables the Controller
 * - Renderable: allows you to render View Model objects.
 */

class MyController extends Controller with ParameterMapper with Validations with Renderable  with FunctionNameConventionRoutes {


  // simple, no args render, just renders the root view of /views/GET/index (or http 204 for JSON)
 // views are resolved by view-root ("/view" on the classpath by default) + HTTP Method + path,
// in this case /views/GET/index. The ending of the template file (mustache, ssp, jade or scaml) will be auto-resolved in the order mentioned here.
// for named params, the ":" of the Scalatra route definition will be replaced by "_" when looking up on the classpath.
 
 
  def `GET /users` = {
    println("GET RECEIVED")        //prints properly to console
    Users.findAll.foreach(println)    //prints properly to console
    render(Users.findAll)
   
  }
 
}

___________________________________________________
 javascript from my html page


defaultAction: function( page ){
                    if( page ) {
                        //Once the default action is called we want to construct a link to our restful service
                        var restfulPageUrl = this.restfulUrl + page //http://localhost:8080/page
                       
                        //Now we have a url lets get the data
                        this.loadRestfulData( restfulPageUrl );
                    }
                },
                loadRestfulData: function( pageUrl ){
                    //Load the data in using jQuerys ajax call
                    $.ajax({
                    beforeSend: function(req) {
                        req.setRequestHeader("Accept", "application/json");
                    },
                        url: pageUrl,
                        dataType: 'json',
                        success: function(data){
                        alert("JSON View =" + data);
                }
                });

_____________________________________________________________-
user class


package bowlerquickstart

import collection.mutable.MutableList

case class Users(id: Long, var name: String, var yearBirth: java.lang.Integer)

object Users{

  var allUsers = new MutableList[Users]
  allUsers += Users(1, "Allan", 2011)
  allUsers += Users(2, "Bob", 1997)

  def findAll: List[Users] = allUsers.toList

}

Wille Faler

unread,
May 9, 2012, 3:43:35 AM5/9/12
to bowler...@googlegroups.com
what HTTP code do you get back?

I'm not sure what your application mappings are, but it looks like your JQuery is looking for something at "/page" whereas your Controller is mapped to "/users"?

rest bowling

unread,
May 9, 2012, 9:33:06 PM5/9/12
to Bowler Users
This is the response header

HTTP/1.1 200 OK
Set-Cookie: JSESSIONID=1yx7dvo8o2t1e6olhfvxnfpf;Path=/
Content-Type: application/json;charset=UTF-8
Content-Length: 81
Server: Jetty(7.4.1.v20110513)

page is a variable that will be equal to 'users' . i have used console
to confirm that the request is being sent to http://localhost:8080/users
on the front end.


On May 9, 3:43 am, Wille Faler <wille.fa...@gmail.com> wrote:
> what HTTP code do you get back?
>
> I'm not sure what your application mappings are, but it looks like your
> JQuery is looking for something at "/page" whereas your Controller is
> mapped to "/users"?
>

Wille Faler

unread,
May 10, 2012, 2:32:16 PM5/10/12
to bowler...@googlegroups.com
Hi,
The content length implies that there is actually some content sent (it's set by Jetty, not Bowler), does the length seem to be reasonable for what you expect to see as a response?

I would suspect that your JavaScript might have a problem, but it's difficult to say without the whole app available. 
Is there any chance you might make a quick start sbt project with a minimal set of the code that "just works" if I clone it off a github repository or something?
Reply all
Reply to author
Forward
0 new messages