成功部分不在.ajax()中工作

时间:2011-11-01 01:35:35

标签: jquery

我有一个名为generateHtml的递归函数,它在表单提交函数中调用。

我遇到success部分的问题。即使我删除成功部分的内容并且只是提醒,它也不起作用。它也没有出现任何错误。

function generateHtml(count,total,pars,files) {

    if (count == 1) {
        f = 'f=';
        allfiles = files;
        allpars = pars;
    }

    fn = randomToN(99999999, 0);
    f = f + fn + '|';
    dat = allpars + "&file=" + allfiles[count] + "&fn=" + fn + '&i=' + count;
    alert(dat);

    $.ajax({
        url: "../../ii/getindreports.php",
        type: 'POST',
        data: dat,
        async:true,
        dataType: 'html',
        error: function () {
            alert('ERROR !!!!');
        },
        complete: function () {
            alert('test');
            //  -------------------------Success part---------------//
            $('#merobar').html( $('#merobar').html() + '<br>Report '+ count +' Compiled.');

            // if count not equal to total we still have html to process so call the function again
            if (count != total) {
                count = count + 1;
                alert(count);
                generateHtml(count, total, 0, 0);
            } else {
                alert('make pdf now ');
                fnpdf = randomToN(99999999, 0);
                f = f + '&pdf=' + fnpdf;

                $.ajax({
                    url: "../../ii/makebulkhtmlpdf.php",
                    type: 'POST',
                    data: f,
                    async:true,
                    dataType: 'html',
                    success: function () {
                        //alert('<br>Report Generation Completed');
                        $('#merobar').html( $('#merobar').html() + '<br>Report Generation Completed');
                        $('#merobar').html( $('#merobar').html() + '<br><a href=\"../../temp/temp_'+fnpdf+'.pdf\" target=\"_blank\">Click To Download</a>');
                    }
                });
            }//end of else
                    //  -------------------------Success part--------------//
        }//end of success
    });// end of  first .ajax function
    return true;
}//end of function

$('#sliderform').submit(function() {
    //alert("here");
    var files = new Array();
    var checked = $("input:checkbox[name=files]:checked").length > 0;

    if (!checked) {
        alert("Please select at least one Report");
        return false;
    }

    i = 1;
    $("input:checkbox[name=files]:checked").each(function () {
        //alert($(this).val());
        if ($(this).val() != '') {
            files[i] = $(this).val();
            i = i + 1;
        }
    });//end of .each

    //alert(files);
    $('#merobar').html('File Processing ...');
    pars = $(this).serialize();
    total = $("input:checkbox[name=files]:checked").length;

    generateHtml(1,total,pars,files);
});//end of submit function

1 个答案:

答案 0 :(得分:0)

我不知道这会做什么,但我将所有代码重写为更接近我做事的方式,添加了良好的风格实践并适当地声明了不是全局的变量。

除了丢失的var关键字之外,我没有看到任何语法错误,因此如果某些内容无效,可能会出现其他代码,其中包含语法问题,导致帖子中的代码无法运行。

function generateHtml(count, total, pars, files) {

    var f = '';
    var allfiles = [];
    var allpars = [];
    var fn = 0;

    if (count == 1) {
        f = 'f=';
        allfiles = files;
        allpars = pars;
    }

    fn = randomToN(99999999, 0);
    f = f + fn + '|';
    dat = allpars + "&file=" + allfiles[count] + "&fn=" + fn + '&i=' + count;
    alert(dat);

    $.ajax({
        url: "../../ii/getindreports.php",
        type: 'POST',
        data: dat,
        async: true,
        dataType: 'html',
        error: function () {
            alert('ERROR !!!!');
        },
        complete: function () {
            alert('test');
            //  -------------------------Success part---------------//
            $('#merobar').append('<br>Report '+ count +' Compiled.');

            // if count not equal to total we still have html to process so call the function again
            if (count != total) {
                count = count + 1;
                alert(count);
                generateHtml(count, total, 0, 0);
            } else {
                alert('make pdf now ');
                var fnpdf = randomToN(99999999, 0);
                f = f + '&pdf=' + fnpdf;

                $.ajax({
                    url: "../../ii/makebulkhtmlpdf.php",
                    type: 'POST',
                    data: f,
                    async:true,
                    dataType: 'html',
                    success: function () {
                        //alert('<br>Report Generation Completed');
                        $('#merobar').append('<br>Report Generation Completed');
                        $('#merobar').append('<br><a href=\"../../temp/temp_' + fnpdf + '.pdf\" target=\"_blank\">Click To Download</a>');
                    }
                });
            }//end of else
                    //  -------------------------Success part--------------//
        }//end of success
    });// end of  first .ajax function
    return true;
}//end of function

$('#sliderform').submit(function() {
    //alert("here");
    var files = [];
    var checked = $("input[name=files]:checked").length > 0;

    if (!checked) {
        alert("Please select at least one Report");
        return false;
    }

    var i = 1;
    $("input[name=files]:checked").each(function () {
        //alert($(this).val());
        if ($(this).val() != '') {
            files.push($(this).val());
        }
    });//end of .each

    //alert(files);
    $('#merobar').html('File Processing ...');
    var pars = $(this).serialize();
    var total = $("input[name=files]:checked").length;

    generateHtml(1, total, pars, files);
});//end of submit function