Is jQuery able to read JSON data from X-JSON HTTP headers returned by the server? I’ve been searching through the jQuery docs, but all the examples I can find use JSON returned in the request body rather than the headers.
Share
Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.
Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.
Lost your password? Please enter your email address. You will receive a link and will create a new password via email.
Please briefly explain why you feel this question should be reported.
Please briefly explain why you feel this answer should be reported.
Please briefly explain why you feel this user should be reported.
Yes, you need to call the
getResponseHeadermethod of theXMLHttpRequestobject, and do the JSON de-serialization manually:Note that the
try/catchis for some versions of Firefox where if the header is not present an error is thrown. I can’t remember which version(s) were affected.You have a couple ways to get a reference to the
XMLHttpRequestobject in jQuery:hook into the
completecallback of the ajax request, as opposed to the expectedsuccesscallback (jQuery is kind of inconsistent wrt to what args are passed in what order to what callback function or global ajax trigger):Alternatively you can save a reference to the
XMLHttpRequestobject returned to you from calls to.ajax/.get/.postetc, via a Closure. This allows you to use it inside whatever callback you choose (iesuccessorcomplete, orerrorfor that matter):So to answer your title directly: no, jQUery obviously doesn’t support this OOTB.