views:

228

answers:

2

I have a controller that returns either JSON or partial html depending on if the user is logged in.

Is there anyway to tell the difference once the result has been returned?? What is the best way.

Obviously i have different actions to complete if the response is json or if it is the partial html. With the json one i do a redirect to the login page, and with the html one i just display it. If only i could render the html server side and send it back in json format?? From what i have heard this cannot be done as yet. Thoughts?

Cheers.

DUPLICATE: http://stackoverflow.com/questions/291466/can-i-evaluate-the-response-type-of-an-ajax-call-in-success-callback

A: 

I'd say, assuming you have control of the thing returning JSON or partial HTML, that you should change the Content-Type header of the JSON result to something like application/x-format-json or an equally obvious type.

Jeff Hubbard
+1  A: 

You could use JSON.js to find out if a given string is JSON or not.

JSON encoder / decoder
This object uses good practices to encode/decode every kind of JSON compatible variable quickly and a bit safer.

It provides a decode() function that returns either an object or undefined.

Tomalak