forked from jquery/jquery-migrate
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathajax-jsonp-callback-name.html
87 lines (77 loc) · 2.23 KB
/
ajax-jsonp-callback-name.html
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>jQuery Migrate test for re-using JSONP callback name with `dataType: "json"`</title>
<!-- Load a jQuery and jquery-migrate plugin file based on URL -->
<script src="testinit.js"></script>
<script>
TestManager.loadProject( "jquery", "git" );
</script>
<script src="iframeTest.js"></script>
<script>
jQuery.noConflict();
TestManager.loadProject( "jquery-migrate", "dev", true );
</script>
<script>
var thisCallbackInWindow1, thisCallbackInWindow2,
previousCallback, previousJsonpCallback,
nextJsonpCallback, error,
requestSucceeded = true;
jQuery.migrateEnablePatches( "jsonp-promotion" )
jQuery.ajaxTransport( "script", function( options ) {
if ( options.url.indexOf( 'fakeScript' ) === -1 ) {
return;
}
return {
abort: function () {},
send: function ( headers, completeCallback ) {
var script = options.jsonpCallback + "( { answer: 42 } )";
setTimeout( function() {
completeCallback( 200, 'OK', { text: script }, [] );
} );
}
};
} );
jQuery.ajax( {
url: "fakeScript1.js?callback=?",
dataType: "json",
beforeSend: function( jqXHR, s ) {
s.callback = s.jsonpCallback;
thisCallbackInWindow1 = this.callback in window;
}
} ).then( function() {
var previous = this;
previousJsonpCallback = previous.jsonpCallback;
thisCallbackInWindow2 = this.callback in window;
return jQuery.ajax( {
url: "fakeScript2.js?callback=?",
dataType: "json",
beforeSend: function() {
previousCallback = previous.callback;
nextJsonpCallback = this.jsonpCallback;
// Test cleanup
delete window.customJsonpCallback;
}
} );
} ).catch( function( _jqXHR, _textStatus, err ) {
if ( !( err instanceof Error ) && _jqXHR instanceof Error ) {
err = _jqXHR;
}
// Test cleanup in case of an error
delete window.customJsonpCallback;
requestSucceeded = false;
error = err;
} ).then( function() {
startIframeTest(
thisCallbackInWindow1, thisCallbackInWindow2,
previousJsonpCallback, previousCallback,
nextJsonpCallback, requestSucceeded, error
);
} );
</script>
</head>
<body>
<p>jQuery Migrate test for re-using JSONP callback name</p>
</body>
</html>