WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

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) {
            type: 'POST',
            url: WP_LOCALIZED['url'],
            data: {
                action:     'request_handler',
                method:     method,
                data:       values
            }, success: function(data) {
                var response = jQuery.parseJSON(data);

            }, 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


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

    header('Content-Type: application/zip');
    header('Content-disposition: attachment; filename=filename.zip');
    header('Content-Length: ' . filesize($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
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'];

    echo json_encode($data);

share|improve this answer
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
@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

Your Answer


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.