1 | define([
|
2 | "../core",
|
3 | "./var/nonce",
|
4 | "./var/rquery",
|
5 | "../ajax"
|
6 | ], function( jQuery, nonce, rquery ) {
|
7 |
|
8 | var oldCallbacks = [],
|
9 | rjsonp = /(=)\?(?=&|$)|\?\?/;
|
10 |
|
11 |
|
12 | jQuery.ajaxSetup({
|
13 | jsonp: "callback",
|
14 | jsonpCallback: function() {
|
15 | var callback = oldCallbacks.pop() || ( jQuery.expando + "_" + ( nonce++ ) );
|
16 | this[ callback ] = true;
|
17 | return callback;
|
18 | }
|
19 | });
|
20 |
|
21 |
|
22 | jQuery.ajaxPrefilter( "json jsonp", function( s, originalSettings, jqXHR ) {
|
23 |
|
24 | var callbackName, overwritten, responseContainer,
|
25 | jsonProp = s.jsonp !== false && ( rjsonp.test( s.url ) ?
|
26 | "url" :
|
27 | typeof s.data === "string" && !( s.contentType || "" ).indexOf("application/x-www-form-urlencoded") && rjsonp.test( s.data ) && "data"
|
28 | );
|
29 |
|
30 |
|
31 | if ( jsonProp || s.dataTypes[ 0 ] === "jsonp" ) {
|
32 |
|
33 |
|
34 | callbackName = s.jsonpCallback = jQuery.isFunction( s.jsonpCallback ) ?
|
35 | s.jsonpCallback() :
|
36 | s.jsonpCallback;
|
37 |
|
38 |
|
39 | if ( jsonProp ) {
|
40 | s[ jsonProp ] = s[ jsonProp ].replace( rjsonp, "$1" + callbackName );
|
41 | } else if ( s.jsonp !== false ) {
|
42 | s.url += ( rquery.test( s.url ) ? "&" : "?" ) + s.jsonp + "=" + callbackName;
|
43 | }
|
44 |
|
45 |
|
46 | s.converters["script json"] = function() {
|
47 | if ( !responseContainer ) {
|
48 | jQuery.error( callbackName + " was not called" );
|
49 | }
|
50 | return responseContainer[ 0 ];
|
51 | };
|
52 |
|
53 |
|
54 | s.dataTypes[ 0 ] = "json";
|
55 |
|
56 |
|
57 | overwritten = window[ callbackName ];
|
58 | window[ callbackName ] = function() {
|
59 | responseContainer = arguments;
|
60 | };
|
61 |
|
62 |
|
63 | jqXHR.always(function() {
|
64 |
|
65 | window[ callbackName ] = overwritten;
|
66 |
|
67 |
|
68 | if ( s[ callbackName ] ) {
|
69 |
|
70 | s.jsonpCallback = originalSettings.jsonpCallback;
|
71 |
|
72 |
|
73 | oldCallbacks.push( callbackName );
|
74 | }
|
75 |
|
76 |
|
77 | if ( responseContainer && jQuery.isFunction( overwritten ) ) {
|
78 | overwritten( responseContainer[ 0 ] );
|
79 | }
|
80 |
|
81 | responseContainer = overwritten = undefined;
|
82 | });
|
83 |
|
84 |
|
85 | return "script";
|
86 | }
|
87 | });
|
88 |
|
89 | });
|