Display loading image while post with ajax

前端 未结 7 1800
野趣味
野趣味 2020-12-07 17:18

I know there are thousands of examples on the internet, but I want for the script I already have to display a loading gif image while the data is retrievedd. My java knowled

相关标签:
7条回答
  • 2020-12-07 17:44

    This is very simple and easily manage.

    jQuery(document).ready(function(){
    jQuery("#search").click(function(){
        jQuery("#loader").show("slow");
        jQuery("#response_result").hide("slow");
        jQuery.post(siteurl+"/ajax.php?q="passyourdata, function(response){
            setTimeout("finishAjax('response_result', '"+escape(response)+"')", 850);
                });
    });
    
    })
    function finishAjax(id,response){ 
          jQuery("#loader").hide("slow");   
          jQuery('#response_result').html(unescape(response));
          jQuery("#"+id).show("slow");      
          return true;
    }
    
    0 讨论(0)
  • 2020-12-07 17:48

    make sure to change in ajax call

    async: true,
    type: "GET",
    dataType: "html",
    
    0 讨论(0)
  • 2020-12-07 17:52
    $.ajax(
    {
        type: 'post',
        url: 'mail.php',
        data: form.serialize(),
        beforeSend: function()
        {
            $('.content').html('loading...');
        },
        success: function(data)
        {
            $('.content').html(data);
        },
        error: function()
        {
            $('.content').html('error');
        }
    });
    

    have fun playing arround!

    if you should have quick loading times which prevent te loading showing, you can add a timeout of some sort.

    0 讨论(0)
  • 2020-12-07 17:53

    Let's say you have a tag someplace on the page which contains your loading message:

    <div id='loadingmessage' style='display:none'>
      <img src='loadinggraphic.gif'/>
    </div>
    

    You can add two lines to your ajax call:

    function getData(p){
        var page=p;
        $('#loadingmessage').show();  // show the loading message.
        $.ajax({
            url: "loadData.php?id=<? echo $id; ?>",
            type: "POST",
            cache: false,
            data: "&page="+ page,
            success : function(html){
                $(".content").html(html);
                $('#loadingmessage').hide(); // hide the loading message
            }
        });
    
    0 讨论(0)
  • 2020-12-07 17:54
    <div id="load" style="display:none"><img src="ajax-loader.gif"/></div>
    
    function getData(p){
            var page=p;
            document.getElementById("load").style.display = "block";  // show the loading message.
            $.ajax({
                url: "loadData.php?id=<? echo $id; ?>",
                type: "POST",
                cache: false,
                data: "&page="+ page,
                success : function(html){
                    $(".content").html(html);
            document.getElementById("load").style.display = "none";
                }
            });
    
    0 讨论(0)
  • 2020-12-07 17:54

    //$(document).ready(function(){
    //	 $("a").click(function(event){
    //		event.preventDefault();
    //		$("div").html("This is prevent link...");
    //	});
    //});			
    
    $(document).ready(function(){
    	$("a").click(function(event){
    		event.preventDefault();
    		$.ajax({
    			beforeSend: function(){
    				$('#text').html("<img src='ajax-loader.gif' /> Loading...");
    			},
    			success : function(){
    				setInterval(function(){ $('#text').load("cd_catalog.txt"); },1000);
    			}
    		});
    	});
    });
    <script src="https://ajax.googleapis.com/ajax/libs/jquery/1.12.4/jquery.min.js"></script>
    		
    <a href="http://www.wantyourhelp.com">[click to redirect][1]</a>
    <div id="text"></div>

    0 讨论(0)
提交回复
热议问题