2

I have trouble with the headers of a simple call of a backbone collection fetch using a service stack backend.

the returned response looks like a json but is just a raw string and backbone don't fetch the collection.

I have notice that the fetch call always result in a error, this is odd since a curl request of the same URL let me check that is all good and I reviving a 200 OK code.

Edited: added the complete response

A call of curl and the received output

the header of the response

> GET /tickets?format=json HTTP/1.1
> User-Agent: curl/7.25.0 (i386-pc-win32) libcurl/7.25.0 zlib/1.2.5
> Host: 192.168.1.50
> Accept: */*
>
* HTTP 1.0, assume close after body
< HTTP/1.0 200 OK
< Date: Tue, 19 Jun 2012 13:09:35 GMT
< Server: Mono.WebServer.XSP/2.8.0.0 Win32NT
< X-AspNet-Version: 4.0.30319
< X-Powered-By: ServiceStack/3,69 Win32NT/.NET
< Set-Cookie: ss-id=URB3Ss34BE2FBliYDXEpBQ==; path=/
< Set-Cookie: ss-pid=HAfI68bmXU6jRdTiH0WEBA==; expires=Sat, 19-Jun-2032 13:06:26
 GMT; path=/
< Cache-Control: private, no-cache="Set-Cookie"
< Content-Type: application/json; charset=utf-8
< Content-Length: 459
< Keep-Alive: timeout=15, max=100
* HTTP/1.0 connection set to keep alive!
< Connection: Keep-Alive

The json received as raw string

[{"id":1,"urgencia":"Normal","fecha":"\/Date(-62135580600000- 0430)\/","ultimaActualizacion":"\/Date(-62135580600000-0430)\/","titulo":"Error 101","descripcion":"Al encender el equipo retorna error 101","status":"A","causas":[],"adjuntos":[],"operadores":[],"citas":[],"estados":[],"comentarios":[{"id":1,"fecha":"\/Date(-62135580600000-0430)\/","editado":"\/Date(-62135580600000-0430)\/","descripcion":"este es un comentario","status":" ","comentarios":[]}]}]

So how do I set the headers in service stack so the backbone collection understand that is a json response?

Update: I think I have found the root of the problem it has something to do with my Nhibernate models as I can't use them directly for the Rest service, any other model will parse to the correct type. but still I don't know why. will look on this.

thanks for your help.

Jesus
  • 633
  • 3
  • 13
  • Check [this thread](http://stackoverflow.com/questions/477816/the-right-json-content-type) - see if that works first. – chrisfrancis27 Jun 18 '12 at 21:22
  • @ChrisFrancis indeed the "Content-Type:application/json" is show in the header of the response but for backbone is just a raw string, at this point I am not sure if this is a problem of the Rest framework wich I doubt or if is a problem of the ajax request :-( – Jesus Jun 18 '12 at 21:30
  • I haven't used backbone so I don't know how it does model binding, but a raw string would suggest that nothing is parsing the JSON response. I mean, serialized JSON **is** just a string after all... – chrisfrancis27 Jun 18 '12 at 21:35
  • Can you post your backbone code that calls the service? – chrisfrancis27 Jun 18 '12 at 21:35
  • actually is just a call for fetch but I am handling the callbacks error and success as it follows collection.fetch({success: onSuccess, error:onError}) you can read more about it [here](http://documentcloud.github.com/backbone/#Collection-fetch) – Jesus Jun 18 '12 at 21:43
  • @JesúsBracamonte mind posting the JSON response for reference. Don't strip anything as i have to inspect if this is valid json Backbone expects – Deeptechtons Jun 19 '12 at 08:36
  • @Deeptechtons done, I also post the header of the response. – Jesus Jun 19 '12 at 14:53
  • It definitely appears that the service stack portion isn't returning an actual JSON response, it's just a string as mentioned above. – jonezy Jun 19 '12 at 15:00

1 Answers1

0

I have found the problem is related with this, is not a header problem as @jonezy told me "even if the header is correct that doesn't mean the response itself is.", Service Stack can't serialize correctly models associated with Nhibernate so I have to do something like this.

A similar problem can be found here

thanks to all for your help.

Community
  • 1
  • 1
Jesus
  • 633
  • 3
  • 13