Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Retrieve script content from jQuery.getScript or jQuery.ajax with dataType script

According to jQuery's documentation, setting dataType: 'script' in $.ajax

Evaluates the response as JavaScript and returns it as plain text.

I got the evaluation part working fine but the response I get is undefined, not a plain text version of the javascript. I have tried this with $.ajax:

$.ajax({
  url:url,
  dataType: 'script',
  success:function(data){
    //data is undefined but script runs as expected 
  }
});

I also tried using $.getScript with the same result:

$.getScript(url, function(data){
    //data is null but script runs as expected 
});
like image 311
Omar Meky Avatar asked Mar 15 '26 09:03

Omar Meky


1 Answers

jQuery will only return the body of the script if it's on the same domain the request originated from.

You can see that it works by going to http://jquery.com and running this in your JS console:

$.getScript('/jquery-wp-content/themes/jquery/js/main.js', function(data){
  console.log(data);
});
like image 57
idbehold Avatar answered Mar 18 '26 07:03

idbehold



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!