How to upload an image through jQuery?

前端 未结 6 810
悲&欢浪女
悲&欢浪女 2020-12-09 12:11

For past few days i have been struggling to submit a form with jQuery and AJAX. The problem i\'m facing is to upload the image in the form field.

My form is somethin

6条回答
  •  北海茫月
    2020-12-09 12:49

    Please check the follow the code, which i am using to upload image.

    $.ajax({
                url: UPLOADURL,   // Url to which the request is send
                type: "POST",       // Type of request to be send, called as method
                data:  new FormData(this),// Data sent to server, a set of key/value pairs representing form fields and values
                contentType: false,// The content type used when sending data to the server. Default is: "application/x-www-form-urlencoded"
                cache: false,// To unable request pages to be cached
                processData:false,// To send DOMDocument or non processed data file it is set to false (i.e. data should not be in the form of string)
                success: function(data)// A function to be called if request succeeds
                {
    
                    data = JSON.parse(data);
                    console.log(data);
                    if(data.status == "Success"){
                        attachmentListing();
                        //$("#mailerMessage").html(data.data.mailStatus);
                        //$("#mailerMessage").fadeIn();
                        setTimeout(function () {
                            $("#mailerMessage").fadeOut();
                        },5000);
                    }else{
                        toastr.warning(data.status);
    
                    }
                    $("#ajaxloader").addClass("hideajaxLoader");
                },
                error: function (jqXHR, errdata, errorThrown) {
                    log("error");
                    $("#ajaxloader").addClass("hideajaxLoader");
                }
            });
    

提交回复
热议问题