Serialize file type jQuery

后端 未结 4 1586
长发绾君心
长发绾君心 2021-02-08 08:34

I\'m trying to serialize my form data including file image field using jquery.form.js jQuery API. The API is helping me to serailze data fields including image

4条回答
  •  隐瞒了意图╮
    2021-02-08 08:52

    HTML

    
    
    
    

    Javascript (note that any methods following self are instance methods, I use Joose)

    $(form).ajaxSubmit({//note that form is just the form built with a jQuery selector
    
                url: self.returnBaseULR() + '/ajaxadd',
    
                type: 'POST',
    
                error: function(xhr, status, error) {
                    console.log('Unable to contact the server');
                },
    
                success: function(response){
    
                    var jsObjectFromResponse = JSON.parse(response);
    
                    if(jsObjectFromResponse.success){
    
                        self.cLog('Item uploaded successfully!');
                        document.location.reload();
    
                    } else {
    
                        self.cLog('Listing failed, see AJAX response');
    
                    }
    
                }
    
            });
    

    You can't upload images using just jQuery native methods. Check out http://jquery.malsup.com/form/

    It has methods that'd do this for you perfectly.

    That just appears to work for me. On the back end I can grab POST params with $_POST and files with $_FILES (or something like that)

    Looks like ajaxSubmit instantly posts the form with serialized data done automatically.

    Hope that helps.

提交回复
热议问题