【问题标题】:File upload with jquery form plugin使用 jquery 表单插件上传文件
【发布时间】:2011-09-01 07:37:16
【问题描述】:

我有一个输入类型=“文件”的表单。它使用 ajax 提交(插件jquery form)。 服务器返回 json 响应。 json数据中有html标签:

{"logs":"<span>vfdvf<\/span>","errors":"<span><\/span>"}

但当插件收到此响应时,它会传输进来

{"logs":"<span>vfdvf&lt;\/span&gt;","errors":"<span>&lt;\/span&gt;"}</span></span>

它不是正确的 json。我该如何解决?如果表单中没有 input type="file" 元素,则一切正常。

这里是JS

$('#edit_ext_table_form').ajaxForm({
    dataType: 'html',
    success: function(responseText) {
        console.log(responseText);
    },
    error: function(request) {
        var responseText=request.responseText;
        console.log(responseText);
    }
}

这里是 PHP

$a = array(
    'logs' => '<span>vfdvf</span>', 
    'errors' => '<span></span>',
);
exit(json_encode($a));

【问题讨论】:

    标签: javascript jquery jquery-forms-plugin


    【解决方案1】:

    您不能通过 ajax 提交文件,Html 5 具有更好的文件上传功能。但在旧浏览器中这是不可能的。不确定这是否正是破坏您的 json 的原因,但您的最终目标是无法实现的。

    【讨论】:

    • 我错了,是 iframe。 “ajax 表单插件”使用 iframe 上传文件
    • 是的,这是进行 ajax 文件上传的公认标准黑客之一,您将输入类型 = 文件放在 iframe 中,然后当用户单击上传时,您使用 javascript 在 iframe 中提交表单在你的文件输入周围
    【解决方案2】:

    你可以试试json dataType

    试试

    $('#edit_ext_table_form').ajaxForm({
    dataType: 'json',
    success: function(result) {
        console.log(result.logs);
        console.log(result.errors);
    },
    failure: function(result) {
        console.log(result.logs);
        console.log(result.errors);
    }});
    

    【讨论】:

    • 有同样的反应。如果 dataType json 则调用函数失败,因为它不正确 json
    【解决方案3】:

    帮助

    json_encode($a, JSON_HEX_TAG)
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2011-08-23
      • 2013-01-28
      • 2013-03-04
      • 2010-12-16
      • 2012-02-24
      • 2013-03-21
      • 2013-01-20
      相关资源
      最近更新 更多