Thanks for your reply, David. > i am afraid you have no way to parse the "Content-Type" in your > javascript. because it the the header of the response, and you can > only get the body (the content) of the response.
This is not true. The XMLHttpRequest object has a getResponseHeader function, which you can call like this: xhr.getResponseHeader('Content- Type'). > actually, for a ajax to be able to work correctly, you MUST guarantee > the request you send with ajax can get what you want, and you always > know beforehand what the format (content type) it would be. Not necessarily. After you receive the response, you can still check whether it is an object or a string (i.e. html). By doing so, you can then decide what to do with it. > for jquery, if you do not specify the content type parameter, it is > smart enough to figure out what it is. Could you please elaborate on this? When jquery figures out the content type of the response, what does it use that information for? As far as I can tell, it doesn't eval it, if the content type is 'application/json'. On Dec 18, 12:47 pm, Xi Shen <davidshe...@googlemail.com> wrote: > i am afraid you have no way to parse the "Content-Type" in your > javascript. because it the the header of the response, and you can > only get the body (the content) of the response. > > actually, for a ajax to be able to work correctly, you MUST guarantee > the request you send with ajax can get what you want, and you always > know beforehand what the format (content type) it would be. > > for jquery, if you do not specify the content type parameter, it is > smart enough to figure out what it is. > > > > On Fri, Dec 18, 2009 at 6:50 AM, MrM <j...@merhar.si> wrote: > > Hi, > > > Firstly, let me apologize if this has been discussed before. I have > > searched the group and couldn't find anything on the subject. > > > Now, to my question. Am I correct to assume that the jQuery.ajax > > function pays no heed to the Content-Type response header it receives > > from the server? If so, can I ask why? For instance, if the server > > sends 'application/json', it is fairly safe to assume that the > > response will be in JSON format and should therefore be eval'd. > > > I have a situation where I do not know beforehand, what type of data > > the server will return, and therefore cannot specify a dataType > > parameter. In this case, I would like jQuery to parse the response, if > > it receives a 'Content-Type' header of 'application/json', and only > > then. If the header received is 'text/html', the response should not > > be parsed. > > > Is there a reason why the 'Content-Type' response header is being > > ignored, when the dataType parameter is not specified? Or is this not > > the case and I am missing something? > > > Thanks! > > -- > Best Regards, > David Shen > > http://twitter.com/davidshen84/http://meme.yahoo.com/davidshen84/