views:

286

answers:

2

i want to know if the response is the json or xml, how can i find that out using jquery?

my current code is following, but its giving error, if response is json.

var is_xml = ($('status_code', XMLHttpRequest.responseText)) ? true : false;

error is:

Selector expected.

Expected ',' or '{' but found '"}}"'.

+1  A: 

Why is the return format not consistent? IMO you should know exactly what should come back before even requesting the page.

meder
because some of the methods are using xml and some json, because of some reason and this was implemented on ajax_error, so ajax error gets trigger if there was any error or a bad response, it will show bad response from xml/json response. :)any solution please!
Basit
Well then why can't you define *two* functions to do the ajax part, one for json and one for xml?
meder
why make two methods, when you can get the job done with one. anyway its solved, thank you for getting involve :)
Basit
Well, the answer chosen was pretty much what I was trying to say in that you had to know what you were working with to send the dataType which is before you receive it...
meder
@meder, I agree with you. +1
Ken Browning
+2  A: 

According to the documentation, the third parameter to an ajaxError callback is the options object which was passed into the call to jQuery.ajax. You can query the dataType value of that object to determine if the call was expected to return json or xml. Of course, this assumes you set the dataType option appropriately to begin with.

Ken Browning