php - 即使操作成功后,加载程序仍保持加载状态

标签 php jquery ajax

将图像添加到字段中时,即使该过程完成后,加载程序仍保持加载状态。我已经使用 ajax 提交了表单。我能知道无限加载的原因是什么吗?我的代码如下

  $('.port_load').waitMe({
        effect: 'win8',
        text: 'Please Wait...',
        bg: 'rgba(255,255,255,0.85)',
        color: '#000',
        maxSize: '',
        source: 'img.svg',
        onClose: function() {}
}); 

这是我提交的完整代码

  $('#port_form').submit(function(e){    
e.preventDefault();    
$('.port_load').waitMe({
        effect: 'win8',
        text: 'Please Wait...',
        bg: 'rgba(255,255,255,0.85)',
        color: '#000',
        maxSize: '',
        source: 'img.svg',
        onClose: function() {}
}); 
if($('#img1').val()!='' && $('#p_img').val()==''){//for editing
    $.ajax({
                url:'../api_fle/port',
                method:'POST',
                data:{u_id:$('#uId').val(),title:$('#p_title').val(),desc:$('#p_description').val(),
                    link:$('#img1').val(),pId:$('#p_id').val()},
                success:function(data){
                      $('.port_load').waitMe('hide');                          
                    if(data==0){
                        $('#wrng').html('<p style="text-align: center; color:red;">Something went Wrong! Try again.</p>');
                    }else{
                                $('#wrng').html('');
                                $('#myModal5').modal('hide');
                                $("#p_img").replaceWith($("#p_img").val('').clone(true));
                                $('#p_title').val('');
                                $('#p_description').val('');
                                $('#p_id').val('');
                                $('#img1').val('');
                        swal({   
                            title: "Success",   
                            text: "Successfully Inserted",   
                            timer: 1000,   
                            showConfirmButton: false
                        });

                        on_load();
                    } 
                }
            });
}else{// adding new
    $.ajax({
        url:'../api_fle/img_upload',
        method:'POST',
        data:  new FormData(this),
        contentType: false,
        cache: false,
        processData:false,
        success:function(data){ 
            if(data==0){
                $('.port_load').waitMe('hide');             
                $('#wrng').html('<p style="text-align: center; color:red;">Something went Wrong! Try again</p>');
            }else if(data==1){
                $('.port_load').waitMe('hide');             
                $('#wrng').html('<p style="text-align: center; color:red;">Upload Image only!</p>');
            }else{                  
                $('#wrng').html("");
                var link="http://www.udyomitra.com/photos/"+$('#uId').val()+"/gallery/"+data;

                $.ajax({
                    url:'../api_fle/port',
                    method:'POST',
                    data:{u_id:$('#uId').val(),title:$('#p_title').val(),desc:$('#p_description').val(),
                        link:link,pId:$('#p_id').val()},
                    success:function(data){

                         $('.port_load').waitMe('hide');    
                        if(data==0){
                            $('#wrng').html('<p style="text-align: center; color:red;">Something went Wrong! Try again</p>');
                        }else{
                            $('#wrng').html("");
                            $('#myModal5').modal('hide'); 
                                $("#p_img").replaceWith($("#p_img").val('').clone(true));
                                $('#p_title').val('');
                                $('#p_description').val('');
                                $('#p_id').val('');
                                $('#img1').val('');   

                            swal({   
                                title: "Success",   
                                text: "Successfully Inserted",   
                                timer: 1000,   
                                showConfirmButton: false 
                            });
                            on_load();
                        } 
                    }
                });
            }

        }   
    });
}


});

最佳答案

直接使用

$('.port_load').waitMe('hide');

更具体地解决您的问题

$(document).ready(function(){
    $(document).ajaxStart(function(){
        $('.port_load').waitMe({
            effect: 'win8',
            text: 'Please Wait...',
            bg: 'rgba(255,255,255,0.85)',
            color: '#000',
            maxSize: '',
            source: 'img.svg',
            onClose: function() {}
        }); 
    });
    $(document).ajaxStop(function(){
        $('.port_load').waitMe('hide');
    });


    $('#port_form').submit(function(e) {
        e.preventDefault();        
        if ($('#img1').val() != '' && $('#p_img').val() == '') { //for editing
            $.ajax({
                url: '../api_fle/port',
                method: 'POST',
                data: {
                    u_id: $('#uId').val(),
                    title: $('#p_title').val(),
                    desc: $('#p_description').val(),
                    link: $('#img1').val(),
                    pId: $('#p_id').val()
                },
                success: function(data) {                    
                    if (data == 0) {
                        $('#wrng').html('<p style="text-align: center; color:red;">Something went Wrong! Try again.</p>');
                    } else {
                        $('#wrng').html('');
                        $('#myModal5').modal('hide');
                        $("#p_img").replaceWith($("#p_img").val('').clone(true));
                        $('#p_title').val('');
                        $('#p_description').val('');
                        $('#p_id').val('');
                        $('#img1').val('');
                        swal({
                            title: "Success",
                            text: "Successfully Inserted",
                            timer: 1000,
                            showConfirmButton: false
                        });
                        on_load();
                    }
                }
            });
        } else { // adding new
            $.ajax({
                url: '../api_fle/img_upload',
                method: 'POST',
                data: new FormData(this),
                contentType: false,
                cache: false,
                processData: false,
                success: function(data) {
                    if (data == 0) {                        
                        $('#wrng').html('<p style="text-align: center; color:red;">Something went Wrong! Try again</p>');
                    } else if (data == 1) {                        
                        $('#wrng').html('<p style="text-align: center; color:red;">Upload Image only!</p>');
                    } else {
                        $('#wrng').html("");
                        var link = "http://www.udyomitra.com/photos/" + $('#uId').val() + "/gallery/" + data;
                        $.ajax({
                            url: '../api_fle/port',
                            method: 'POST',
                            data: {
                                u_id: $('#uId').val(),
                                title: $('#p_title').val(),
                                desc: $('#p_description').val(),
                                link: link,
                                pId: $('#p_id').val()
                            },
                            success: function(data) {          
                                if (data == 0) {
                                    $('#wrng').html('<p style="text-align: center; color:red;">Something went Wrong! Try again</p>');
                                } else {
                                    $('#wrng').html("");
                                    $('#myModal5').modal('hide');
                                    $("#p_img").replaceWith($("#p_img").val('').clone(true));
                                    $('#p_title').val('');
                                    $('#p_description').val('');
                                    $('#p_id').val('');
                                    $('#img1').val('');
                                    swal({
                                        title: "Success",
                                        text: "Successfully Inserted",
                                        timer: 1000,
                                        showConfirmButton: false
                                    });
                                    on_load();
                                }
                            }
                        });
                    }
                }
            });
        }
    });

});

Reference

关于php - 即使操作成功后,加载程序仍保持加载状态,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41828962/

相关文章:

php - 如何使用 PHP 在 MySQL 数据库中存储和检索图像?

php - 使用 PHP 中的 PKCS12 证书 (.p12) 签署 XML 文档 (XADES-BES)

jquery - 如何超越 Web 框架来创建自己的应用程序框架?

javascript - 单击 React-router 中的链接时,组件获得错误的 props

javascript - Vuejs Bootstrap 选择文本不更新

PHP - 在视频上传期间提取帧

javascript - 延迟 AngularJS 中的整个页面加载,直到服务的 ajax 完成

javascript - 使用 Ajax 仅检索数据库中的数据之间如何更好地从 html 结构中的数据库检索数据?

jquery - 如何使用 Ajax 将图像发送到 PHP 文件?

php - `print_r($mysqli,1)` 更改为 `$mysqli->affected_rows `