简体   繁体   English

使用jQuery表单插件上传文件

[英]File upload with jquery form plugin

i have a form with input type="file". 我有一个输入type =“ file”的表单。 it submits using ajax (plugin jquery form ). 它使用ajax提交(插件jquery形式 )。 Server returns json response. 服务器返回json响应。 There are html tags in json data: json数据中有html标签:

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

but when plugin gets this response it transferred in 但是,当插件收到此响应时,它将转移到

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

it is not correnct json. 它不是正确的json。 How can i fix it? 我该如何解决? If there is no input type="file" element in form, all works fine. 如果表单中没有输入type =“ file”元素,则一切正常。

Here is JS 这是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 这是PHP

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

You cannot submit a file via ajax, Html 5 has much better file upload capabilities. 您无法通过Ajax提交文件,HTML 5具有更好的文件上传功能。 But in older browsers its not possible. 但是在较旧的浏览器中是不可能的。 Not sure if thats exactly what is breaking your json, but your end goal is unachievable. 不知道那是不是真的破坏了json,但是最终目标是无法实现的。

maby you can try json dataType . maby,您可以尝试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);
}});

有帮助

json_encode($a, JSON_HEX_TAG)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM