From: rwldrn Date: Wed, 19 Jan 2011 21:29:42 +0000 (-0500) Subject: [#270] Add XHR API dataType requirement for specific object returns X-Git-Tag: v0.3~25^2 X-Git-Url: http://git.khamidou.com/?a=commitdiff_plain;h=742ed140bbafc94a08a0024346c62eeff8407d07;p=popcorn-js.git [#270] Add XHR API dataType requirement for specific object returns --- diff --git a/popcorn.js b/popcorn.js index 3b5abbc..c1957c3 100644 --- a/popcorn.js +++ b/popcorn.js @@ -813,7 +813,7 @@ Popcorn.xhr = function ( options ) { - if ( ( options.dataType || "" ).toLowerCase() === "jsonp" ) { + if ( options.dataType && options.dataType.toLowerCase() === "jsonp" ) { Popcorn.xhr.getJSONP( options.url, @@ -823,9 +823,14 @@ } var settings = Popcorn.extend( {}, setup, options ); - + + // Create new XMLHttpRequest object settings.ajax = settings.xhr(); + // Normalize dataType + settings.dataType = settings.dataType.toLowerCase(); + + if ( settings.ajax ) { if ( settings.type === "GET" && settings.data ) { @@ -865,6 +870,12 @@ text: settings.ajax.responseText, json: json }; + + // If a dataType was specified, return that type of data + if ( settings.dataType ) { + data = data[ settings.dataType ]; + } + settings.success.call( settings.ajax, data ); @@ -881,13 +892,23 @@ script = document.createElement("script"), paramStr = url.split("?")[1], fired = false, - params, callback; - - script.src = url; + params = [], + callback; - params = paramStr.split("&"); + if ( paramStr ) { + params = paramStr.split("&"); + } + + + callback = params.length ? params[ params.length - 1 ].split("=")[1] : "jsonp"; + + + if ( !paramStr ) { + url += "?callback=" + callback; + } + + script.src = url; - callback = params.length ? params[ params.length - 1 ].split("=")[1] : Popcorn.guid("jsonp"); if ( callback ) { // define the jsonp success callback globally @@ -909,7 +930,7 @@ head.insertBefore( script, head.firstChild ); - } + }; // Exposes Popcorn to global context diff --git a/test/index.html b/test/index.html index db5f979..d5af739 100644 --- a/test/index.html +++ b/test/index.html @@ -4,6 +4,8 @@ Popcorn API + +