Deprecated Ajax Or JQuery Command Cause No Result Returned

…衆ロ難τιáo~ 提交于 2019-12-12 04:38:12

问题


<script type="text/javascript">
        $(document).ready(function(){
            function loading_show(){
                $('#loading').html("<img src='images/loading.gif'/>").fadeIn('fast');

            }
            function loading_hide(){
                $('#loading').fadeOut('fast');

            }                
            function loadData(page){
                loading_show();    

                $.ajax
                ({
                    type: "POST",
                    url: "load_data.php",
                    data: "page="+page,
                    success: function(msg){
                        $("#container").ajaxComplete(function(event, request, settings)//.ajaxComplete is good
                        {
                            loading_hide();
                            $("#container").html(msg);
                        });
                    }
                });
                 alert();

            }
            loadData(1);  // For first time page load default results
            $('#container .pagination li.active').on('click',function(){//replaced .live with .on
                var page = $(this).attr('p');
                loadData(page);

            });           
            $('#go_btn').on('click',function(){//replaced .live with .on
                var page = parseInt($('.goto').val());
                var no_of_pages = parseInt($('.total').attr('a'));
                if(page != 0 && page <= no_of_pages){
                    loadData(page);
                }else{
                    alert('Enter a PAGE between 1 and '+no_of_pages);
                    $('.goto').val("").focus();
                    return false;
                }

            });
        });
    </script>

Above is the pagination code from The paginatio tutorial here. I try to use jquery 1.9.1 instead of the code piece use 1.4. But seem even I changed the deprecated .live to .on, still, no result has been rendered. I can not find any deprecated code among this piece. However, other code in the tutorial are seemingly perfect as well. So I would consider there must be some thing wrong with the javascript & ajax code above.

with the help from KyleK, the modified version (yet still not working) is below

<script type="text/javascript">
        $(document).ready(function(){
            function loading_show(){
                $('#loading').html("<img src='images/loading.gif'/>").fadeIn('fast');
            }
            function loading_hide(){
                $('#loading').fadeOut('fast');
            }                
            function loadData(page){
                loading_show();                    
                $.ajax
                ({
                    type: "POST",
                    url: "load_data.php",
                    data: {page: page},
                    //ORIGINAL data: "page="+page,
                    success: function(msg)
                    {
                        $("#container").ajaxComplete(function(event, request, settings)
                        {
                            loading_hide();
                            $("#container").html(msg);
                        });
                    }
                });
            }
            loadData(1);  // For first time page load default results
            $('body').on('click','#container .pagination li.active', function(){
            //ORIGINAL $('#container .pagination li.active').live('click',function(){
                var page = $(this).attr('p');
                loadData(page);

            });           
            $('body').on('click','#go_btn',function(){
            //ORIGINAL $('#go_btn').live('click',function(){
                var page = parseInt($('.goto').val());
                var no_of_pages = parseInt($('.total').attr('a'));
                if(page != 0 && page <= no_of_pages){
                    loadData(page);
                }else{
                    alert('Enter a PAGE between 1 and '+no_of_pages);
                    $('.goto').val("").focus();
                    return false;
                }

            });
        });
    </script>

回答1:


Change the way your data is passed in the ajax...

From...

url: "load_data.php",
data: "page="+page,

To..

url: "load_data.php",
data: {page: page},

ANd change this...

 $('#container .pagination li.active').on('click',function(){

To this...

 $('body').on('click','#container .pagination li.active', function(){

And same here...

 $('#go_btn').on('click',function()

To...

 $('body').on('click','#go_btn',function()

I think I already suggested that the last time I answered this same question yesterday...




回答2:


here is basic difference between live and on you can check

$('selector').live(event, function(){ //do stuff here })

and in on

$(document).on(event, selector, function(){ //do stuff here })

And also you can refer detail Documentation to get better idea to convert it

let me know if i can help you more.



来源:https://stackoverflow.com/questions/17174237/deprecated-ajax-or-jquery-command-cause-no-result-returned

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!