0

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&lt;\/span&gt;","errors":"<span>&lt;\/span&gt;"}</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));

3 Answers 3

1

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.

Sign up to request clarification or add additional context in comments.

2 Comments

i was wrong, it is iframe. "ajax form plugin" uploads files using iframe
yes thats one of the accepted standard hacks for doing an ajax file upload you put the input type = file in an iframe and then when the user clicks upload you use javascript to submit the form in the iframe which is around your file input
1

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

1 Comment

there is the same response. if dataType json then calls function failure bacause it is not correct json
1

Helps

json_encode($a, JSON_HEX_TAG)

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.