1

I'm trying to access a cross domain .js file using ajax:

$.ajax({
    type:'get',
    crossDomain:true,
    url: "http://localhost/62588/scripts/bootStrapper.js",
    contentType: "application/json",
    dataType: 'jsonp'    
}).done(callback);

I have a callback at the moment:

getBootStrapperScript(function (callback) {         
       //do somethibg
})

I get the following error:

XMLHttpRequest cannot load http://localhost/62588/scripts/bootStrapper.js. Origin http://localhost:62607 is not allowed by Access-Control-Allow-Origin.

I have been reading about JSONP but I'm not sure how to use it to load a .js file from anoather domain.

If I change the above code to use 'jsonp' for the datatype but I then get this errer:

GET http://localhost/62588/scripts/bootStrapper.js?callback=jQuery18206067646441515535_1354459693160&_=1354459696966 404 (Not Found) 

How can I load cross domain js files?

5
  • 3
    looks like you are victim of same origin policy Commented Dec 2, 2012 at 14:57
  • This may help you, stackoverflow.com/questions/6114436/… Commented Dec 2, 2012 at 14:58
  • 2
    There's a special function that does this for you, it's called $.getScript(), and it get's the external javascript file and inserts it. An other way would be to look at how Google does it with stuff like the analytics file, inserting a script tag into the head, and do something like that. Using an Ajax call with jsonp is NOT the way to do it. Commented Dec 2, 2012 at 15:00
  • Why the down vote? Genuinely interested so I can provide better questions in future :) Commented Dec 2, 2012 at 16:02
  • Thanks all - see comment on Darins answer. Commented Dec 2, 2012 at 16:35

1 Answer 1

6

Don't use any AJAX, simply use the $.getScript function:

$.getScript('http://localhost/62588/scripts/bootStrapper.js').done(callback);

As you know, you could be pointing the <script> tag to any domain you wish without violating the same origin policy. That's the basis of JSONP. But you don't need any JSONP, because all you need is to reference a script form a remote domain, which is as simple as pointing the <script> tag to this script or if you want to do it dynamically use the $.getScript function that jQuery has to offer you.


UPDATE:

The $.getScript function will append a random cache busting parameter to the url. If you want to get a cached version of your script you could define a custom cachedScript function as shown in the documentation:

jQuery.cachedScript = function(url, options) {
    options = $.extend(options || {}, {
        dataType: 'script',
        cache: true,
        url: url
    });
    return jQuery.ajax(options);
};

and then:

$.cachedScript('http://localhost/62588/scripts/bootStrapper.js').done(callback);
Sign up to request clarification or add additional context in comments.

5 Comments

thanks, I still get the error: GET localhost/62588/scripts/bootStrapper.js?_=1354460928278 404 (Not Found). :(
What happens when you type http://localhost/62588/scripts/bootStrapper.js?_=1354460928278 in your browser address bar? Is the script served? Or do you get 404? If the script is served without an error I would be greatly surprised. If you get 404, well, then, make sure you have provided a correct url to your script in the $.getScript function - one that actually exists on your server at the specified address :-)
yeah, 404 but with only localhost:62588/Scripts/bootStrapper.js - the script is served.
Weird, your web server doesn't serve the script if there are query string parameters? The _=1354460928278 bit is to ensure that you get a fresh version of the script from the server. If you don't want this random query string parameter to be appended to the url you could set the cache: false parameter. Read the documentation of the $.getScript function I've linked to in my answer. There's a section called Caching Responses at the end. Look at the example provided there. They define a custom jQuery.cachedScript function. I have updated my answer to provide an example.
Actually - I'm an idiot: Real reason - look at the url: $.getScript('localhost/62588 the '/' after localhost should actually be a ':' - thanks Darin.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.