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?
Don’t use any AJAX, simply use the
$.getScriptfunction: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$.getScriptfunction that jQuery has to offer you.UPDATE:
The
$.getScriptfunction 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 customcachedScriptfunction as shown in the documentation:and then: