0

I have a clojure running server whose one endpoint returns simple json.

I make a callback from backbone.js collection to this endpoint. I have put a done handler on the return xhr of the fetch() of collection. However, this done callback is never called. I can see that request is 200 with right data coming to the client.

code is here:

(defn getJson []
  {:status 200
   :headers {"Content-Type" "application/json"}
   :body "[{id: 123, name: \"a2k jailed\"}, {id: 234, name: \"klmno won\"}]"
   }
)

(defroutes main-routes*
  (GET "/headlines" [] (getJson))
  (route/resources "/")
  (route/not-found "<h1>404 - Page not found</h1>"))

(def app (compojure.handler/site main-routes*))

(defn -main [port]
  (def server (jetty/run-jetty #'app
   {:port (Integer. port) :join? false})))


// backbone client side:
this.jsonCollection  = new someClassExtendedFrombackboneCollection();
this.jsonCollection.fetch().done(function(jsons) {
    console.log("jsons: " + jsons);
});

What is not right in here ?

Ashish Negi
  • 5,193
  • 8
  • 51
  • 95
  • 2
    This may or may not be the problem but Json requires all keys to be quoted and the top level collection must be an object and may not be an array. You might want to use a json generator like cheshire instead of writing json strings by hand. – Joost Diepenmaat Jul 14 '14 at 18:01
  • 2
    Does it log anything if you change `done` to `fail`? – glortho Jul 14 '14 at 19:13
  • I wanna cry.. i failed on every frontier of programming. I could have saved my 2 days if i had written a fail. – Ashish Negi Jul 15 '14 at 04:37
  • if i send back an object then there should be a array of models in it. what should be my key for that array for backbone to know that is models ? – Ashish Negi Jul 15 '14 at 04:42

0 Answers0