Javascript Jquery-加载动画问题
我试图在AJAX调用完成后显示加载图像 目前我有以下Javascript代码:Javascript Jquery-加载动画问题,javascript,jquery,ajax,Javascript,Jquery,Ajax,我试图在AJAX调用完成后显示加载图像 目前我有以下Javascript代码: window.onload = function(){ var img = document.createElement("IMG"); img.src = "http://i.stack.imgur.com/FhHRx.gif"; function getCategories(){ $.ajax({ url: //url here, dataType:'jsonp',
window.onload = function(){
var img = document.createElement("IMG");
img.src = "http://i.stack.imgur.com/FhHRx.gif";
function getCategories(){
$.ajax({
url: //url here,
dataType:'jsonp',
success: function(data){
//code here
},
statusCode: {
404: function() {
alert:('There was a problem with the server');
}
}
});
}
$(document).ajaxStart(function(){
alert('here');
var div = document.createElement("div");
div.id = "id1";
document.getElementById('id1').appendChild(img);
document.body.appendChild(div);
}).ajaxStop(function(){
var element = document.getElementById('id1');
element.parentNode.removeChild(element);
});
但是,我发现代码甚至没有到达ajaxStart函数,因为警报没有出现。。。我已经尝试过其他方法,例如:尝试让这项工作,但尚未成功到目前为止
请问有谁能帮忙吗
多谢各位 由于您错误地声明了一个函数,您很可能会收到意外令牌的错误
Uncaught SyntaxError: Unexpected token {
您需要声明一个具有以下任一项的函数:
function getCategories () { }
或:
我想我在这里找到了解决办法 似乎ajaxStart只在使用XMLHttpRequest时才起作用。如果使用JSONP,则不会使用XMLHttpRequest。相反,它插入了一个标签来实现这一目标
我只是使用了上面链接中建议的解决方法。控制台有错误吗?真的很抱歉。。。但我确实正确地声明了函数。我在复制代码时出错了。。。
var getCategories = function () { };