使用jquery表单插件上传文件

发布于 12-02 19:24 字数 968 浏览 5 评论 0原文

我有一个输入类型=“文件”的表单。它使用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));

i have a form with input type="file". it submits using ajax (plugin jquery form).
Server returns json response. There are html tags in json data:

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

but when plugin gets this response it transferred in

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

it is not correnct json. How can i fix it? If there is no input type="file" element in form, all works fine.

Here is JS

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

Here is PHP

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

魔法少女2024-12-09 19:24:46

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

You cannot submit a file via ajax, Html 5 has much better file upload capabilities. But in older browsers its not possible. Not sure if thats exactly what is breaking your json, but your end goal is unachievable.

怪我太投入2024-12-09 19:24:46

也许你可以尝试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);
}});

maby you can try json dataType.

Try

$('#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);
}});
夏尔2024-12-09 19:24:46

帮助

json_encode($a, JSON_HEX_TAG)

Helps

json_encode($a, JSON_HEX_TAG)
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文