Get Ajax PHP response for file upload form
<p>i've created a upload form with text fields to create custom posts from frontend in Wordpress. </p>

<p>The requst is working, but i can't get the response message in ajax when a file is uploaded. If no file is uploaded and only the text field is set then i get the response.</p>

<p>here is my ajax form:</p>

<pre><code>//upload-form.js
_submit: function (event) {
        event.preventDefault();

        this.$submitButton.prop('disabled', true);

        var $formdata = false;
        var $form = this.$form;

        if (window.FormData) {
            $formdata = new FormData();
        }
        var $files_data = this.$upload;

        if ($files_data.val() == '') {
            $formdata.append('fields', $form.serialize());
        } else {
            $.each($($files_data), function (i, obj) {
                $.each(obj.files, function (j, file) {
                    $formdata.append('files[' + j + ']', file);
                    $formdata.append('fields', $form.serialize());
                })
            });
        }

        $formdata.append('action', 'upload_form_submit');
        $formdata.append('nonce', upload.form_nonce);

        $.ajax({
            url: upload.ajaxurl,
            type: 'POST',
            data: $formdata,
            dataType: "json",
            async: false,
            success: this._success.bind(this),
            error: this._error.bind(this),
            cache: false,
            contentType: false,
            processData: false
        });

        return false;
    },

    _success: function (jsonResponse) {

        var response = jsonResponse;

        if (response.type == 'success') {

            // Clear fields
            this.$fields.val('');
            this.$submitButton.prop('disabled', true);

            // Show message
            if (response.message) {
                $('.response-success').text(response.message);
            }
        } else {
            this._error(response.message);
        }

        return jsonResponse;
    },

    _error: function (error) {

        this.$submitButton.prop('disabled', false);

        // Show message
        if (error) {

            if (typeof error === 'object') {
                $('.response-success').text(error.statusText);
            } else {

                // Custom error
                $('.response-success').text(error);

                this.$form.find('*[required]').each(function (i, elem) {
                    var $elem = $(elem);
                    if (!$elem.val()) {
                        $elem.parent().addClass('empty-field');
                    }
                });
            }

        }
    }
</code></pre>

<p>My PHP response</p>

<pre><code>/**
 * Callback to validate AJAX request
 */
public function ajax_submit_form() {
    check_ajax_referer( 'form_submit', 'security' );

    if ( !isset( $_POST['fields'] ) ) {
        return;
    }

    $json = array();

    // Parse $.serialize()
    parse_str( $_POST['fields'], $this-&gt;_post_fields );

    // Check if required fields are not empty
    if ( $this-&gt;is_valid_data() ) {
        // save posts
        if ( $this-&gt;handle_frontend_new_post_form_submission() ) {
            $json['type']    = 'success';
            $json['message'] = $this-&gt;_notices['post_sent'];
        } else {
            $json['type']    = 'error';
            $json['message'] = $this-&gt;_notices['post_not_sent'];
        }
    } else {
        $json['type']    = 'error';
        $json['message'] = $this-&gt;_notices['empty_fields'];
    }


    die( wp_json_encode( $json ) );
}
</code></pre>