2013-09-29 37 views
2
$.ajax({ 
    url: "test.html", 
    context: document.body 
}).done(function() { 
    //show data 
}); 

如何将数据从ajax请求保存到变量中,然后显示它? 以及如何在处理过程中显示消息“loading ...”?如何将ajax调用请求数据保存在变量中并显示它?

+0

功能(数据){...}); ?? – MightyPork

+0

http://stackoverflow.com/questions/905298/jquery-storing-ajax-response-into-global-variable –

回答

1
$.ajax({ 
    url: "test.html", 
    context: document.body 
}).done(function(data) { 
    alert(data); 
}); 

更新:

$.ajax({ 
    url: "test.html", 
    context: document.body 
}).done(function(data) { 
    $('#loading').hide();  
    // alert(data); 
}); 

标记:

<div id='loading'></div> 
+0

可能的重复感谢如何在请求发送时显示“加载...”消息? –

1
对于

加载消息,在AJAX使用beforeSend()。

beforeSend : function() { 
     $('body').html('Loading...'); 
    } 
1

你可以尝试这样的事情。在你的函数可以显示加载DIV

function your_ajax() { 
    $('#loading').show(); 
    $.ajax({ 
    url: "test.html", 
    context: document.body 
    }).done(function(data) { 
    $('#loading').hide();  
    alert(data); 

    }); 
} 

请在你的HTML添加此部分

<div id="loading" style="display:none;">Loading </div> 
+0

谢谢@Shafeeq –

+0

如何在超时请求中包装这个例子,例如每5秒刷新一次? –

相关问题