Take the 2-minute tour ×
WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. It's 100% free, no registration required.

I'm creating an application that will allow a user to fill out a form and then have a customized zip file downloaded for them upon submit. I'm utilizing AJAX to accomplish this task.

    Build.prototype.ajaxHandler = function(method, values) {
        jQuery.ajax({
            type: 'POST',
            url: WP_LOCALIZED['url'],
            data: {
                action:     'request_handler',
                method:     method,
                data:       values
            }, success: function(data) {
                var response = jQuery.parseJSON(data);
                console.log(response);

            }, error: function(XMLHttpRequest, textStatus, errorThrown) {
                console.log(XMLHttpRequest, textStatus, errorThrown);
            }
        });
    };

Whenever I try to return basic data with my ajax handler I am able to successfully return without issue, but when I try to process my zip file method I consistently get Uncaught SyntaxError: Unexpected token < even though the zip_file() method is not actually returning any data back to the javascript method.

/**
 * AJAX handler
 * 
 * @return {json}
 * @since 1.0
 */
public function request_handler()
{

    $post = $_POST;
    $data = $post['data'];

    // If this wasn't here it would return my $data variable
    $this->zip_file();

    die(json_encode($data));
}

I've also tested the zip_file() method on its own by placing it in the init() function and it runs just as I would expect it to without issue.

/**
 * Create Zip File
 * 
 * @return void
 * @since 1.0
 */    
public function zip_file()
{
    $zip = new ZipArchive;

    if($zip->open('test.zip', ZipArchive::CREATE) === TRUE) {
        if($zip->addEmptyDir($this->_plugin_path . 'assets/temp_files')) {
            // Do stuff
        }
        $zip->close();
    }

    header('Content-Type: application/zip');
    header('Content-disposition: attachment; filename=filename.zip');
    header('Content-Length: ' . filesize($zip));
    readfile($zip);
}

Interestingly the same thing happens if I replace the $this->zip_file() call with just a basic echo statement. Except instead of telling me the Unexpected token is < it says e instead.

I think I'm misunderstanding something fundamental about the way these two interact together, but so far have not been able to locate any information on what that may be.

Any help would be appreciated.

share|improve this question
add comment

1 Answer

up vote 2 down vote accepted

Try to clear the buffer before returning your AJAX response.

    public function request_handler() {

    $post = $_POST;
    $data = $post['data'];

    $this->zip_file();
    ob_clean();
    echo json_encode($data);

    die();
}
share|improve this answer
1  
Well we're halfway there :). I added the ob_clean per your suggestion and the ajax doesn't fail now so I'm going to accept this answer. The $this->zip_file method however still does not run as I would expect it to. I tried adding return 'test' to the end of the method and I am able to successfully retrieve that result, but none of the other functionality (Zipping up the file and downloading) is executing. Any ideas? Thanks for the help! –  voodooGQ Jun 21 '13 at 3:01
1  
@voodooGQ You said that if you put the $this->zip_file method into the init hook, it works as expected. Is that still the case? I would make sure you are creating the zip archive file, returning early if you cannot, and make sure you return the zip archive at the end of the method (instead of just reading it). Example try something like this: gist.github.com/rachelbaker/d8de958ff5cfe63c6d12/… –  Rachel Baker Jun 21 '13 at 12:23
    
The zip_file was returning true, but I found out that I had an extra folder in the path that wasn't created yet so although it was passing it wouldn't actually save the file. All good to go now! Thanks again Rachel –  voodooGQ Jun 21 '13 at 19:29
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.