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.

    评论

报告相同问题?

悬赏问题

  • ¥15 关于#Java#的问题,如何解决?
  • ¥15 加热介质是液体,换热器壳侧导热系数和总的导热系数怎么算
  • ¥15 想问一下树莓派接上显示屏后出现如图所示画面,是什么问题导致的
  • ¥100 嵌入式系统基于PIC16F882和热敏电阻的数字温度计
  • ¥15 cmd cl 0x000007b
  • ¥20 BAPI_PR_CHANGE how to add account assignment information for service line
  • ¥500 火焰左右视图、视差(基于双目相机)
  • ¥100 set_link_state
  • ¥15 虚幻5 UE美术毛发渲染
  • ¥15 CVRP 图论 物流运输优化