You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 
 
 

101 lines
2.6 KiB

  1. define( [
  2. "../core",
  3. "./var/nonce",
  4. "./var/rquery",
  5. "../ajax"
  6. ], function( jQuery, nonce, rquery ) {
  7. var oldCallbacks = [],
  8. rjsonp = /(=)\?(?=&|$)|\?\?/;
  9. // Default jsonp settings
  10. jQuery.ajaxSetup( {
  11. jsonp: "callback",
  12. jsonpCallback: function() {
  13. var callback = oldCallbacks.pop() || ( jQuery.expando + "_" + ( nonce++ ) );
  14. this[ callback ] = true;
  15. return callback;
  16. }
  17. } );
  18. // Detect, normalize options and install callbacks for jsonp requests
  19. jQuery.ajaxPrefilter( "json jsonp", function( s, originalSettings, jqXHR ) {
  20. var callbackName, overwritten, responseContainer,
  21. jsonProp = s.jsonp !== false && ( rjsonp.test( s.url ) ?
  22. "url" :
  23. typeof s.data === "string" &&
  24. ( s.contentType || "" )
  25. .indexOf( "application/x-www-form-urlencoded" ) === 0 &&
  26. rjsonp.test( s.data ) && "data"
  27. );
  28. // Handle iff the expected data type is "jsonp" or we have a parameter to set
  29. if ( jsonProp || s.dataTypes[ 0 ] === "jsonp" ) {
  30. // Get callback name, remembering preexisting value associated with it
  31. callbackName = s.jsonpCallback = jQuery.isFunction( s.jsonpCallback ) ?
  32. s.jsonpCallback() :
  33. s.jsonpCallback;
  34. // Insert callback into url or form data
  35. if ( jsonProp ) {
  36. s[ jsonProp ] = s[ jsonProp ].replace( rjsonp, "$1" + callbackName );
  37. } else if ( s.jsonp !== false ) {
  38. s.url += ( rquery.test( s.url ) ? "&" : "?" ) + s.jsonp + "=" + callbackName;
  39. }
  40. // Use data converter to retrieve json after script execution
  41. s.converters[ "script json" ] = function() {
  42. if ( !responseContainer ) {
  43. jQuery.error( callbackName + " was not called" );
  44. }
  45. return responseContainer[ 0 ];
  46. };
  47. // Force json dataType
  48. s.dataTypes[ 0 ] = "json";
  49. // Install callback
  50. overwritten = window[ callbackName ];
  51. window[ callbackName ] = function() {
  52. responseContainer = arguments;
  53. };
  54. // Clean-up function (fires after converters)
  55. jqXHR.always( function() {
  56. // If previous value didn't exist - remove it
  57. if ( overwritten === undefined ) {
  58. jQuery( window ).removeProp( callbackName );
  59. // Otherwise restore preexisting value
  60. } else {
  61. window[ callbackName ] = overwritten;
  62. }
  63. // Save back as free
  64. if ( s[ callbackName ] ) {
  65. // Make sure that re-using the options doesn't screw things around
  66. s.jsonpCallback = originalSettings.jsonpCallback;
  67. // Save the callback name for future use
  68. oldCallbacks.push( callbackName );
  69. }
  70. // Call if it was a function and we have a response
  71. if ( responseContainer && jQuery.isFunction( overwritten ) ) {
  72. overwritten( responseContainer[ 0 ] );
  73. }
  74. responseContainer = overwritten = undefined;
  75. } );
  76. // Delegate to script
  77. return "script";
  78. }
  79. } );
  80. } );