Ajax xhr产生错误

时间:2013-10-29 16:51:50

标签: jquery ajax

我使用完全相同的代码来上传图像和显示进度,但在这种情况下,虽然唯一的变化是上传音频文件但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.

1 个答案:

答案 0 :(得分:0)

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

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

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