]> source.dussan.org Git - jquery.git/commitdiff
Ajax: Allow custom attributes when script transport is used
authorDave Methvin <dave.methvin@gmail.com>
Tue, 12 Sep 2017 16:40:00 +0000 (12:40 -0400)
committerDave Methvin <dave.methvin@gmail.com>
Mon, 14 May 2018 18:09:43 +0000 (14:09 -0400)
Fixes gh-3028
Ref gh-2612

Useful, for example, to add `nonce`, `integrity`, or `crossorigin`.

src/ajax/script.js
test/unit/ajax.js

index 6e0d21e993383f586f58a34635fae856d2c2131e..292627f7ecec26902e9d525427708b786ab90494 100644 (file)
@@ -43,15 +43,15 @@ jQuery.ajaxPrefilter( "script", function( s ) {
 // Bind script tag hack transport
 jQuery.ajaxTransport( "script", function( s ) {
 
-       // This transport only deals with cross domain requests
-       if ( s.crossDomain ) {
+       // This transport only deals with cross domain or forced-by-attrs requests
+       if ( s.crossDomain || s.scriptAttrs ) {
                var script, callback;
                return {
                        send: function( _, complete ) {
                                script = jQuery( "<script>" ).prop( {
                                        charset: s.scriptCharset,
                                        src: s.url
-                               } ).on(
+                               } ).attr( s.scriptAttrs || {} ).on(
                                        "load error",
                                        callback = function( evt ) {
                                                script.remove();
index ff9fd8e7040ec3b6af608ac2db343b596420a211..8a13810fc5b6f031f1657e6c57e30ac4b6548e83 100644 (file)
@@ -89,6 +89,29 @@ QUnit.module( "ajax", {
                }
        );
 
+       ajaxTest( "jQuery.ajax() - custom attributes for script tag", 4,
+               function( assert ) {
+                       var nonceValue = "0123456789";
+                       return {
+                               create: function( options ) {
+                                       var xhr;
+                                       options.dataType = "script";
+                                       options.scriptAttrs = { id: "jquery-ajax-test", nonce: nonceValue };
+                                       xhr = jQuery.ajax( url( "data/script.php?header=ecma" ), options );
+                                       // Ensure the script tag has the nonce attr on it
+                                       assert.ok( nonceValue === jQuery( "#jquery-ajax-test" ).attr( "nonce" ), "nonce value" );
+                                       return xhr;
+                               },
+                               success: function() {
+                                       assert.ok( true, "success" );
+                               },
+                               complete: function() {
+                                       assert.ok( true, "complete" );
+                               }
+                       };
+               }
+       );
+
        ajaxTest( "jQuery.ajax() - do not execute js (crossOrigin)", 2, function( assert ) {
                return {
                        create: function( options ) {