2011-09-01 66 views
0

我有一個輸入類型爲「file」的表單。它提交使用ajax(插件jquery form)。 服務器返回json響應。有在JSON數據的HTML標籤:用jQuery表單插件進行文件上傳

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

但是當插件獲取此響應它

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

轉移是不correnct JSON。我該如何解決它?如果表單中沒有輸入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)); 

回答

1

幫助

json_encode($a, JSON_HEX_TAG) 
1

無法通過AJAX提交文件,HTML 5擁有更好的文件上傳功能。但在舊版瀏覽器中不可能。不確定這究竟是什麼打破了你的JSON,但你的最終目標是無法實現的。

+0

我錯了,它是iframe中。 「ajax表單插件」使用iframe上傳文件 – Ildar

+0

是的,這是一個接受的標準黑客做ajax文件上傳你把輸入類型=文件在iframe中,然後當用戶點擊上傳你使用javascript來提交表單在在你的文件輸入 – kmcc049

1

maby你可以試試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); 
}}); 
+0

周圍的iframe有相同的響應。如果dataType json然後調用函數失敗,因爲它是不正確的json – Ildar

相關問題