【问题标题】:Ajax xhr producing errorAjax xhr 产生错误
【发布时间】:2013-10-29 16:51:50
【问题描述】:

我使用与此完全相同的代码来上传图像并显示进度,但在这种情况下,唯一的变化是上传音频文件,但 xhr 只是停止并产生此错误:

[object Object]an error occurred

html:

<form enctype="multipart/form-data" id="audioForm">
    <p>Click browse to choose the audio file(mp3) from your PC (Maximum file size is 5mb)</p>
    <input type="file" name="audio" value="" class="input-xlarge required"><br />
    <label for="song_title">Song Title</label>
    <input type="hidden" name="act_id" value="<?=$act_id?>">
    <input type="text" class="form-control" id="song_title" name="song_title">
    <input type="button" class="btn btn-info" value="Upload Images" id="uploadAudio" />
</form>

<div id="progress" style="display:none;">
    <progress value="0" max="100"></progress>
</div>
<div id="response" style="display:none;"></div>

JQuery:

$('body').on('click', '#uploadAudio', function(){

        var form = new FormData($('#audioForm')[0]);
        $('#progress').fadeIn();

        // Make the ajax call
        $.ajax({
            url: '/ajax/actions/addAudio.php?act_id=<?=$act_id?>',
            type: 'POST',
            xhr: function() {
                var myXhr = $.ajaxSettings.xhr();
                if(myXhr.upload){
                    myXhr.upload.addEventListener('progress',progress, false);
                }
                return myXhr;
            },
            success: function (res) {
                $('#progress').hide();
                $('#response').show();
                $('#response').html(res);
                $("#audioDisplay").load('/ajax/display/audioDisplay.php?act_id=<?=$act_id?>');
            },
            error: function(res){
                $('#progress').hide();
                $('#response').show();
                $('#response').html(res + 'an error occurred');
            },
            data: form,
            cache: false,
            contentType: false,
            processData: false
         });
});

有什么明显的吗?

新的错误函数:

error: function(jqXHR, textStatus, errorThrown){
    $('#response').html('textStatus: ' + textStatus + '<br>errorThrown: ' + errorThrown);
},

这给出了这个错误:

textStatus: error
errorThrown: TypeError: Argument 2 of EventTarget.addEventListener does not implement interface EventListener.

【问题讨论】:

    标签: jquery ajax


    【解决方案1】:

    你应该使用错误函数的完整形式:

    error: function(jqXHR, textStatus, errorThrown) {
       ...
    }
    

    textStatus 和 errorThrown 将为您提供有关错误性质的更多信息。

    【讨论】:

    • 谢谢,那我该如何显示它们呢?
    • 使用您的代码,您可以执行以下操作: $('#response').html('textStatus: ' + textStatus + '
      errorThrown: ' + errorThrown);
    • 这意味着你的progress对象没有实现Event listener接口。您可以找到更多信息:developer.mozilla.org/en-US/docs/Web/API/… 和 developer.mozilla.org/en-US/docs/Web/API/EventListener
    • 是的,我看到了 - 但是,我在同一个站点的不同页面上使用完全相同的代码并且它工作得很好?那是我无法理解的
    • 我建议您使用 javascript 调试器(chrome 中有一个,或者您可以将 FireFox 与 FireBug 一起使用)来分析您工作页面的代码,并将其与不工作的代码进行比较。我怀疑您的其他页面包含一些其他 javascript,这些 javascript 初始化了您的新页面不包含的进度对象。
    猜你喜欢
    • 1970-01-01
    • 2020-12-26
    • 2014-06-02
    • 2022-01-04
    • 1970-01-01
    • 2014-09-13
    • 1970-01-01
    • 2017-10-16
    • 2015-02-07
    相关资源
    最近更新 更多