Images in this post missing? We recently lost them in a site migration. We're working to restore these as you read this. Should you need an image in an emergency, please contact us at imagehelp@codebetter.com
Enabling auto-detection of response type in jQuery

Sometimes you don't know what type of document the server is going to return, so it would be nice to have the ajax handler deal with the response as the server is directing it to.

JQuery doesn't have this baked in (I have had a ticket for ages there to implement it), but a workaround is to roll your own handler. The data filter that was introduced in the latest round of jquery is useless since you don't have access to the response object (and thus the header) and doing hacky string parsing was brittle. To replace the current handler, just roll your own httpData function that replaces the native function in jquery.

Using jquery 1.2.6 +  here is a patch you can add after the jquery reference on your page:

* patch for dynamic evaluation of xhr responseType to set the dataType on jQuery */
jQuery.extend({
 
httpData: function( xhr, type, s ) {        
       
var ct = xhr.getResponseHeader("content-type"),
               
xml = type == "xml" || !type && ct && ct.indexOf("xml") >= 0,
               
script = type == "script" || !type && ct && ct.indexOf("script") >= 0,
               
json = type == "json" || !type && ct && ct.indexOf("json") >= 0,
               
data = xml ? xhr.responseXML : xhr.responseText;
                
               
if ( xml && data.documentElement.tagName == "parsererror" )
                   
throw "parsererror";
                
               
// Allow a pre-filtering function to sanitize the response
                // s != null is checked to keep backwards compatibility
                if( s && s.dataFilter )
                   
data = s.dataFilter( data, type );
                
               
// If the type is "script", eval it in global context
                if ( script ) 
                   
jQuery.globalEval( data );

               
// Get the JavaScript object, if JSON is used.
                if ( json )
                   
data = eval("(" + data + ")");

               
return data;
   
}
});

 

All this is doing is what it already does with xml response types...it sniffs the response header and handles appropriately. Now I can let my server determine whether I am generating just some javascript snippet that should get evaluated or some markup that should be pushed into the page without requiring my client ajax method to state it explicitly.


Posted 10-14-2008 8:40 PM by Michael Nichols
Filed under:

[Advertisement]

Comments

Dew Drop - October 15, 2008 | Alvin Ashcraft's Morning Dew wrote Dew Drop - October 15, 2008 | Alvin Ashcraft's Morning Dew
on 10-15-2008 8:21 AM

Pingback from  Dew Drop - October 15, 2008 | Alvin Ashcraft's Morning Dew

About The CodeBetter.Com Blog Network
CodeBetter.Com FAQ

Our Mission

Advertisers should contact Brendan

Subscribe
Google Reader or Homepage

del.icio.us CodeBetter.com Latest Items
Add to My Yahoo!
Subscribe with Bloglines
Subscribe in NewsGator Online
Subscribe with myFeedster
Add to My AOL
Furl CodeBetter.com Latest Items
Subscribe in Rojo

Member Projects
DimeCasts.Net - Derik Whittaker

Friends of Devlicio.us
Red-Gate Tools For SQL and .NET

NDepend

SlickEdit
 
SmartInspect .NET Logging
NGEDIT: ViEmu and Codekana
LiteAccounting.Com
DevExpress
Fixx
NHibernate Profiler
Unfuddle
Balsamiq Mockups
Scrumy
JetBrains - ReSharper
Umbraco
NServiceBus
RavenDb
Web Sequence Diagrams
Ducksboard<-- NEW Friend!

 



Site Copyright © 2007 CodeBetter.Com
Content Copyright Individual Bloggers

 

Community Server (Commercial Edition)