Question

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

Was it helpful?

Solution

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.

OTHER TIPS

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?

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top