Я схожу с ума, пытаясь решить эту проблему.Я реализовал Ajax-загрузку файлов, похожую на Coldfusion, которая загружает файл, используя скрытый фрейм, а затем захватывает содержимое, возвращенное фреймом.Это основано на этой статье: http://www.bennadel.com/blog/1244-ColdFusion-jQuery-And-AJAX-File-Upload-Demo.htm
Итак, все работает отлично, ЗА ИСКЛЮЧЕНИЕМ, когда я отправляю обратно HTML в данных.Итак, если вы посмотрите на код ниже, я предоставил страницу действий, которая передает данные обратно в скрытый фрейм.Первая строка работает, вторая строка с html - нет.
<!--- Create the return HTML. Remember, we are going to be treating the BODY of the returned document as if it were a JSON string. --->
<cfsavecontent variable="strHTML">
<cfset var sResponse = {} />
<!--- THIS WORKS --->
<cfset sResponse = {SUCCESS = true, ERRORS = [], DATA = "Hello World", RETURNID=""} />
<!--- THIS DOES NOT WORK --->
<cfset sResponse = {SUCCESS = true, ERRORS = [], DATA = #HtmlEditFormat('<div>Hello World</div>')#", RETURNID=""} />
<cfoutput>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html>
<head></head>
<body>#SerializeJSON( sResponse )#</body>
</html>
</cfoutput>
</cfsavecontent>
<!--- Create binary response data. --->
<cfset binResponse = ToBinary( ToBase64( strHTML ) ) />
<!--- Tell the client how much data to expect. --->
<cfheader name="content-length" value="#ArrayLen( binResponse )#" />
<!--- Stream the "plain text" back to the client .--->
<cfcontent type="text/html" variable="#binResponse#" />
Javascript для выполнения всего этого ниже.Строка с ошибкой при возврате HTML:
// Предполагая, что наши возвращаемые данные в формате JSON, оцените тело html, чтобы получить наши возвращаемые данные.var objData = eval ("(" + jBody.html () + ")");
//ShareForm Photo Upload Form Process (simulated Ajax)
$(document).ready(function() {
// Attach an event to the submit method. Instead of submitting the actual form to the primary page, we are going to be submitting the form to a hidden iFrame that we dynamically create.
$( "#frmShareForm" ).submit(
function( objEvent ){
var jThis = $( this );
var photoUploadAction = $("#photoUploadAction").val();
// Create a unique name for our iFrame. We can do this by using the tick count from the date.
var strName = ("uploader" + (new Date()).getTime());
// Create an iFrame with the given name that does not point to any page - we can use the address "about:blank" to get this to happen.
var jFrame = $( "<iframe name=\"" + strName + "\" src=\"about:blank\" />" );
// We now have an iFrame that is not attached to the document. Before we attach it, let's make sure it will not be seen.
jFrame.css( "display", "none" );
// Since we submitting the form to the iFrame, we will want to be able to get back data from the form submission.
// To do this, we will have to set up an event listener for the LOAD event of the iFrame.
jFrame.load(
function( objEvent ){
// Get a reference to the body tag of the loaded iFrame. We are doing to assume that this element will contain our return data in JSON format.
var objUploadBody = window.frames[ strName ].document.getElementsByTagName( "body" )[ 0 ];
// Get a jQuery object of the body so that we can have better access to it.
var jBody = $( objUploadBody );
// Assuming that our return data is in JSON format, evaluate the body html to get our return data.
var objData = eval( "(" + jBody.html() + ")" );
alert(objData);
// A JSON-format struct is returned that will be used to do callback functionality
SharePhotoAjaxResponseHandler(objData);
// Remove the iFrame from the document. Because FireFox has some issues with "Infinite thinking", let's put a small delay on the frame removal.
setTimeout
(
function()
{
jFrame.remove();
}, 100
);
}
);
// Attach to body.
$( "body:first" ).append( jFrame );
// Now that our iFrame it totally in place, hook up the frame to post to the iFrame.
jThis
.attr( "action", photoUploadAction)
.attr( "method", "post" )
.attr( "enctype", "multipart/form-data" )
.attr( "encoding", "multipart/form-data" )
.attr( "target", strName );
});
});
Яваскрипт, который я получаю, обычно выглядит примерно так: «литерал не определенного регулярного выражения»
Я также попытался удалить SerialiseJson на странице CFM и eval в javascript, что приводит к другим ошибкам js.Я также попытался удалить окружающий html, а также изменить .html () на .text (), удалить eval () и их комбинации.Не повезло.
Пожалуйста, кто-нибудь может сказать мне, в чем проблема.Я знаю, что не могу быть далеко, так как он работает без HTML.
Спасибо
ОБНОВЛЕНИЕ: я запустил вывод JSON, который Coldfusion генерирует через валидатор JSON, и он не состоялся из-за (насколько я вижу) двойных кавычек:
{""RETURNID"":"""",""DATA"":""<div>Hello World<\/div>"",""SUCCESS"":true,""ERRORS"":[]}
Теперь я действительно не знаю, почему Coldfusion делает двойные кавычки ??У кого-нибудь есть идеи?