Json or Html? How to say?

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

Is there a way to tell the difference after returning the result? What's the best way.

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

Greetings.

DUPLICATE: Can I evaluate the response type of the $ .ajax () call on a successful callback?

0


source to share


2 answers


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

JSON encoder / decoder
This object uses good techniques to quickly encode / decode all types of JSON-compliant variables and is slightly more secure.



It provides a function decode()

that returns either an object or undefined

.

+1


source


I would say, assuming that you are in 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 equally obvious.



0


source







All Articles