1

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

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

在javascript中,我们做(简而言之):

$('#images_upload_file').uploadify({

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

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

无论如何,“响应”总是空的。事件、queueID、fileObj 和数据都正确填写。有谁知道如何解决这一问题?

如果您需要更多信息,请与我们联系。PS:我们的代码上传图片就好了,只是我们升级到jquery 1.4后响应一直是空的

4

2 回答 2

1

我总是json2.js用来处理任何 json 数据。这个库有安全机制,以防数据不是正确的 json 格式。你可以从http://json.org获得它,一定要下载 js 文件,不要直接从他们的网站上使用它。

我的代码总是这样:

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 请求还是正常的页面访问。如果所需的会话不满足页面的规则,它将立即重定向,因此它将返回一个完整的网页。

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

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

于 2010-01-17T17:16:36.197 回答
0

是否在 PHP 脚本中打开了 error_reporting?

会不会是因为设置而没有输出的致命错误(例如,因为它无法加载 Zend_Json_Encoder 类)error_reporting

于 2010-01-16T22:12:54.533 回答