I am trying to make an asynchronous call to a server in the following way
$(document).ready(function(){
$.ajax({
cache: true,
async: true,
dataType: "script",
url:"www.xyz/yyy?host_name=abc&size=S&use_flash=YES&use_transparent=YES&lang=en",
success: function(data) {
$("#verified").append(data);
console.log("data is "+data);
loading = false;
}
});
});
However the script is not being loaded asynchronously.What exactly am I missing? Any help would be appreciated!
I am trying to make an asynchronous call to a server in the following way
$(document).ready(function(){
$.ajax({
cache: true,
async: true,
dataType: "script",
url:"www.xyz./yyy?host_name=abc.&size=S&use_flash=YES&use_transparent=YES&lang=en",
success: function(data) {
$("#verified").append(data);
console.log("data is "+data);
loading = false;
}
});
});
However the script is not being loaded asynchronously.What exactly am I missing? Any help would be appreciated!
Share Improve this question edited Dec 29, 2013 at 6:47 Smrita asked Dec 29, 2013 at 6:30 SmritaSmrita 1,2793 gold badges18 silver badges40 bronze badges 2-
1
You're missing
success
to retrieve response from the server! – Dhaval Marthak Commented Dec 29, 2013 at 6:35 - Also, do you see the request happen in the Chrome/Firefox Inspector's Network panel? By the way, you shouldn't use HTML encodings for special characters ("&") in javascript. – tdooner Commented Dec 29, 2013 at 6:36
3 Answers
Reset to default 3Try jQuery.getScript function.
Use Following function for loading javascript asynchronous
function script(url) {
var scriptObject = document.createElement('script');
scriptObject .type = 'text/javascript';
scriptObject .async = true;
scriptObject .src = url;
document.getElementsByTagName('head')[0].appendChild(scriptObject );
}
The "cache" and "async" properties for ajax call have the values "true" by default, you don't have to specify the defaults again.
You mentioned that it takes time for your file to load, that is probably because that the script file you are trying to load is of ample size. By ample size I mean more than 50 or 100 kb.
To reduce the time it takes for your file to load you can minify your js file which will help you reduce the file size of your script and eventually load faster on ajax call.
http://jspress. is one of the good on-line minifier available.
Alternatively, you can try using requirejs library which is widely used for loading script files asynchronously. You can download the requirejs library from the following link: http://requirejs/docs/download.html