Omar Meky
Omar Meky

Reputation: 606

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 
});

Upvotes: 3

Views: 1524

Answers (1)

idbehold
idbehold

Reputation: 17168

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);
});

Upvotes: 1

Related Questions