null return value from json request

524 views
Skip to first unread message

arieljake

unread,
May 11, 2012, 12:26:12 AM5/11/12
to ang...@googlegroups.com
I am returning the string "null" in a response with content-type "json" but I see that angular is returning the "null" string as the data from the request.

The JSON_START reg exp does not account for the possibility of this. Is this a bug?

For reference:

var JSON_START = /^\s*(\[|\{[^\{])/,
      JSON_END = /[\}\]]\s*$/;

Vojta Jína

unread,
May 21, 2012, 12:29:43 AM5/21/12
to ang...@googlegroups.com
Hmmm, I think we should check the headers and do JSON.parse() if
Content-Type is application/json, rather than checking what is the
string look like...

In meantime, you can override the default transformResponse function like this:

$http.defaults.transformResponse = [function(data) {
// replace XSFR protection token
data = data.replace(/^\)\]\}',?\n/, '');

return header('Content-Type') === 'application/json' ?
JSON.parse(data) : data;
}];

V.
> --
> You received this message because you are subscribed to the Google Groups
> "AngularJS" group.
> To view this discussion on the web visit
> https://groups.google.com/d/msg/angular/-/0PXejjIni0gJ.
> To post to this group, send email to ang...@googlegroups.com.
> To unsubscribe from this group, send email to
> angular+u...@googlegroups.com.
> For more options, visit this group at
> http://groups.google.com/group/angular?hl=en.

Vojta Jína

unread,
May 21, 2012, 12:30:49 AM5/21/12
to ang...@googlegroups.com
Sorry, I missed header argument:

$httpProvider.defaults.transformResponse = [function(data, header) {
// replace XSFR protection token
data = data.replace(/^\)\]\}',?\n/, '');

return header('Content-Type') === 'application/json' ?
JSON.parse(data) : data;
}];

Reply all
Reply to author
Forward
0 new messages