Justin John
Justin John

Reputation: 9616

Check the response of ajax in jquery is json or not

How to check the response of ajax in jquery is json or not ?

Sometimes the response will get json format, other times may be in piece of html code or integer.

How to check whether the given response in json format or any other ?

Upvotes: 0

Views: 384

Answers (1)

ThiefMaster
ThiefMaster

Reputation: 318508

By default jQuery is smart and tries to detect the format. You can use various methods to detect the content type, the best one would be looking at the content-type header of the response - but that will only work if your serverside application sends appropriate headers.

However, you should really try to keep the format the same for all responses. Why not simply change whatever does not return json currently to wrap the text in JSON, e.g. {"text": "yourtext"}

Upvotes: 1

Related Questions