views:

759

answers:

2

I'm using uploadify with jquery 1.4 to upload images. In the php script that uploads the files we print this:

$json_response['status'] = "true";
$json_response['file'] = $_FILES;
echo Zend_Json_Encoder::encode($json_response);

In javascript we do (in short):

$('#images_upload_file').uploadify({

    onComplete: function(event, queueID, fileObj, response, data) {

            console.log("upload complete");
            console.log(response);

the "response" is always empty, no matter what. Event, queueID, fileObj and data are all filled up correctly. Does anyone know how to fix this?

Let me know if you need more information. PS: our code uploads images just fine, just the response is empty all the time since we upgraded to jquery 1.4

A: 

Is error_reporting turned on in the PHP script?

Could it be a fatal error (e.g. because it can't load the Zend_Json_Encoder class) that is not output because of the error_reporting setting?

Pekka
Hi Pekka, it's not a fatal error. We're using json_encoder in all our app and that's working just fine.
Jorre
+1  A: 

I always use json2.js to process any json data. This library have safety mechanism in case the data is not in proper json format. You can get it from http://json.org, be sure to download the js file, not using it directly from their site.

My code always looks like this:

onComplete : function (event, queueID, fileObj, response, data) {
  //process response
  try {
    var r = JSON.parse(response);
    //process the JSON data, ex
    console.log(r.status); 
  }
  catch(e) {
    //not json or bad format, do something about it
    alert("cannot parse data as json!");
  }
}

The reason I use json2.js is because my php script have session checking, and will redirect if the session is not accepted. It done before entering the page, using filter module, so I cannot check if it an AJAX request, or normal page access. If the required session is not satisfied the page's rule, it will redirect immediately, so it will return a full web page.

This will make the response is not in valid json format. Using json2.js I can handle it in catch block, then do another action, reloading current page for example. This is just something that I always use, and always working for me.

FYI, json2.js not require and not related with jQuery at all.

Donny Kurnia
thanks this answer is great. It fixes the json parsing problem, although this must be an uploadify issue, since we never had to use json class with jQuery. There is one but: Safari just crashes/closes automatically after uploading a file... Does that work with you on safari?
Jorre
another remark: Internet Explorer 7 shows your alert when parsing the JSON, it works in FF and Safari and Chrome, but not IE... any idea on that?
Jorre
Well, I use linux, so I only check my code in Firefox and Google Chrome. I never got a problems in both. IE always PITA to handle. Have you use the latest uploadify? Maybe it's the bug in uploadify or safari. For IE, if it's up to me, I'd rather display a splash image to tell the user to use 'modern browser' :). Maybe you can use Firebug lite to trace the problems in IE.
Donny Kurnia
This alert in IE, what the text in it? If you use `json2.js` from http://json.org directly, the script in site is displaying the alert to prevent other do it. If you have download it, open it in editor to see if it still contain the alert code.
Donny Kurnia