关于jq ajax封装以及ajax上传Webapi

时间:2023-11-23 15:46:02

jq的ajax完整版本

$(function () {
    fileChange();
});
function fileChange() {
    $('#fileinput').change(function () {
        ajaxUploadImg();
    });
}
function ajaxUploadImg() {
    var formData = new FormData($('#idcardA')[0]);
    $.ajax({
        url: appObj.baseApiUrl + '/userinfo/UploadIDCardInfo',  //server script to process data
        type: 'POST',
        datatype: 'json',
        contentType: false,
        xhr: function () {  // custom xhr
            myXhr = $.ajaxSettings.xhr();
            if (myXhr.upload) { // check if upload property exists
                myXhr.upload.addEventListener('progress', progressHandlingFunction, false); // for handling the progress of the upload
            }
            return myXhr;
        },
        //Ajax事件
        //beforeSend: beforeSendHandler,
        success: function (data) {
            $('#idCardimg').attr('src', appObj.baseUrl + data);
        },
        error: function () {
            alert('error');
        },
        // Form数据
        data: formData,
        //Options to tell JQuery not to process data or worry about content-type
        cache: false,
        processData: false
    });
}
 
 
 
上传身份证正面照片:
                    <form id="idcardA" enctype="multipart/form-data">
                        <input id="fileinput" name="file" type="file" />
                    </form>