简体   繁体   English

如何修复jQuery中的ajax xhr错误

[英]How to fix ajax xhr error in jquery

Basically I have a type="file" , I need to send chosen file through Ajax, But every time the form gets to submit I am getting a console error which says: 基本上,我有一个type="file" ,我需要通过Ajax发送选择的文件,但是每次提交表单时,我都会收到一个控制台错误,内容为:

Error while submitting ajax 提交ajax时出错

here is how I submit myform: 这是我提交myform的方式:

$(document).ready(function() {
$('form').submit(function(event) {
    event.preventDefault();
    checkforImage($('#Propicselecter_file'), "yes");
});
$('[type="file"]').change(function() {
    var fileInput = $(this);
    checkforImage(fileInput, "no");
});
});

function submit_form(){
var fileInput = $('#Propicselecter_file');
$.ajax({
        url: '../propicuploader',
        type: 'POST',
        processData: false,
        data: fileInput[0],
        success: function(data){
        }
    });
}

function checkforImage(fileInput, submitornot){
if (fileInput.length && fileInput[0].files && fileInput[0].files.length) {
    var url = window.URL || window.webkitURL;
    var image = new Image();
    image.onload = function() {
      $("#NotPictureerror_spn").text("");
      if(submitornot == "yes"){
        submit_form();

      }
    };
    image.onerror = function() {
      $("#NotPictureerror_spn").text("Chosen file is not an image, Please Try Again");
    };
    image.src = url.createObjectURL(fileInput[0].files[0]);
}
}

Thanks in advance:) 提前致谢:)

Try passing a formData object instead of the file object: 尝试传递formData对象而不是文件对象:

var form_data = new FormData();
form_data.append('file', fileInput.files[0]);
$.ajax({
     url: '../propicuploader',
     type: 'POST',
     processData: false,
     contentType:false, // Added
     data: form_data,
     success: function(data){
   }
});

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM