本文介绍了uploadComplete之后,uploadify + jQuery 1.4响应为空的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用带有jQuery 1.4的uploadify来上传图像.在上传文件的php脚本中,我们将打印以下内容:

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);

在javascript中(简而言之):

In javascript we do (in short):

$('#images_upload_file').uploadify({

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

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

无论如何,响应"始终为空.事件,queueID,fileObj和数据均正确填写.有人知道如何解决这个问题吗?

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?

如果您需要更多信息,请告诉我. PS:我们的代码上传图片就很好,自从我们升级到jquery 1.4以来,响应一直都是空的

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

推荐答案

我总是使用json2.js处理任何json数据.该库具有安全机制,以防数据不是正确的json格式.您可以从 http://json.org 获取它,请确保下载js文件,而不要直接从中使用它他们的网站.

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.

我的代码总是像这样:

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!");
  }
}

我使用json2.js的原因是因为我的php脚本具有会话检查功能,如果不接受该会话,则会进行重定向.它是在进入页面之前使用过滤器模块完成的,因此我无法检查它是否为AJAX请求或正常的页面访问.如果所需的会话不满足页面的规则,它将立即重定向,因此它将返回完整的网页.

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.

这将使响应不是有效的json格式.使用json2.js,我可以在catch块中对其进行处理,然后执行其他操作,例如重新加载当前页面.这只是我一直使用并一直为我工作的东西.

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.

仅供参考,json2.js不需要,也与jQuery无关.

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

这篇关于uploadComplete之后,uploadify + jQuery 1.4响应为空的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-02 17:50