views:

131

answers:

2

I'm using Ajax file upload function with its javascript / jQuery library.

When uploading a file, I keep getting this error message: SyntaxError: invalid label

This is my JS script:

  jQuery('.uploadImage').live('click',function() {
      ajaxFileUpload();
  });

 (...)

  function ajaxFileUpload(){
    jQuery.ajaxFileUpload({
            url:'../wp-content/plugins/wp-filebrowser/uploader.php', 
            secureuri:false,
            fileElementId:'uploadFile',
            dataType: 'json',
            success: function (data, status){
                if(typeof(data.error) != 'undefined'){
                    if(data.error != ''){
                        alert(data.error);
                    }else{
                        alert(data.msg);
                    }
                }
            },
            error: function (data, status, e){
                alert(data + ' - ' + status + ' - ' + e);
            }
        }
    )
    return false;   
  }

My PHP script works (tested before using json / jquery), but there must be something wrong with my json output from my PHP file. I've tried two approaches.

I'm using json_encode to format the output. This is some of my PHP code:

    (...)
    // Error message is at this stage empty.
    move_uploaded_file($_FILES["file"]["tmp_name"], $uploadfile);
    $respons = $_FILES["file"]["name"]._e(' successfully uploaded');

    $data = array( "error"=> $error, "msg"=> $respons );
    echo json_encode($data);  

UPDATE
It turns out that I was using Worpdress's _e() for supporting multilanguage. The problem is that _e() echo's the content and therefor clutters the JSON response. Once I switched to __() it worked.

Thanks for helping medebug this poblem guys.

A: 

json labels have to enclosed in quotes:

"'error':" . $error . "'\n";

and so on. As well, if $error contains any quotes/colons, that'll "break" the syntax as well. Basically you're wide open to the JSON equivalent of SQL injection with what you're doing. You'd be better off not building JSON strings yourself and simply using json_encode() on normal PHP arrays/objects instead. If you preserve the output of both versions somehow (error_log()?), you can pass them through http://jsonlint.com/ to see what's wrong with them.

Marc B
I've updated my code to use this (with no luck): ` $data[] = array( "error"=> $error, "msg"=> $respons ); echo json_encode($data);`
Steven
Capture the string as it's generated on the server, and compare to what's received by the browser. Possibly something could be fiddling with it in transit (server-side mod_security, client-side antivirus, etc...)
Marc B
+1  A: 

The first approach does not produce valid JSON. Take a look at the output of the json_encode()-function, which generates it correctly. The main problem is that the keys and values are not enclosed by double quotes.

Did you try to use firebug, to identify the exact source of the error? Every JSON-key has to be a string. This is obviously not the case in your faulty line.

elusive
Using double quotes didn't make anny difference. Both `$error` and `$respons` are text strings. I do not get any feedback in Firebug. Is this because dynamic IFrame is created?
Steven
Simply adding double quotes is not going to do the job. I highly recommend using `json_encode()` since it does everything for you. And yes, iframes are like separate browser windows. Firebug only works on the current page.
elusive
I am using `json_encode`. Look at my updated code.
Steven
Aaaah - could it be that I'm using Worpdress `_e()` for language support? I think I'm using it wrong at this stage. It's echoing the text, not adding it to a variable.... will test now.
Steven
That is the reason, i suppose. And i would change `$data[] = array( "error"=> $error, "msg"=> $respons );` to `$data = array( "error"=> $error, "msg"=> $respons );` since you do not need the extra array created by `[]`.
elusive
Yup. It works now. Thanks :)
Steven
You're welcome!
elusive