weixin_33690963 2016-04-23 23:08 采纳率: 0%
浏览 27

jQuery Ajax愚蠢的错误

I have been coding an ajax request and i have a problem with it.

var addonUploadForm = $('#addonUploadForm');

var addonUploadFormMessages = $('#addonUploadForm-messages');

$(addonUploadForm).submit(function(e) {
    e.preventDefault();

    //var formData = $(addonUploadForm).serialize();
    //var formData = new FormData($(this)[0]);
    var formData = new FormData($('#addonUploadForm')[0]);

    $.ajax({
        type: 'POST',
        url: $(addonUploadForm).attr('action'),
        data: formData,
        xhr: function() {  },
        cache: false,
        contentType: false,
        processData: false // marked line of error
        success: function(response) {
            $(addonUploadFormMessages).removeClass('error');
            $(addonUploadFormMessages).addClass('success');

            $(addonUploadFormMessages).html(response);

            $('#addonTitle').val('');
            $('#addonDescription').val('');
            $('#addonFile').val('');
            grecaptcha.reset();
        },
        error: function(data) {
            $(addonUploadFormMessages).removeClass('success');
            $(addonUploadFormMessages).addClass('error');
            grecaptcha.reset();

            if (data.responseText !== '') {
                $(addonUploadFormMessages).html(data.responseText);
            } else {
                $(addonUploadFormMessages).html('<div class="alert alert-danger fade in out"><a href="#" class="close" data-dismiss="alert" aria-label="close" title="close">×</a><strong>Error!</strong> An error occured and your message could not be sent.</div>');
            }
        }
    });
});

That is my code and on the marked line there is a missing , and this code works fine apart from doesnt display the request on the page and it instead just takes me to the ajax url but if i put the , in it does nothing when i submit the form no errors, no nothing.

Thanks Ryan

  • 写回答

2条回答 默认 最新

  • weixin_33692284 2016-04-23 23:25
    关注

    Probably this line causes the error:

    xhr: function() {  },
    

    without an xhr object you cannot send an ajax request. So leave out this line.

    Also you need to put the "," in at your marked line. Your url opens because if you leave out the "," the function will throw an error and your e.preventDefault() won't work.

    Also I would leave out these lines:

    contentType: false,
    processData: false 
    

    And you should probably escape the html content in this line:

    $(addonUploadFormMessages).html(data.responseText);
    

    Hope this helps.

    评论

报告相同问题?