【发布时间】:2011-04-12 16:23:20
【问题描述】:
我要疯了,试图解决这个问题。我已经实现了一个类似于 Coldfusion Ajax 的文件上传,它使用隐藏框架上传文件,然后抓取框架返回的内容。基于这篇文章: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格式,评估body 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 );
});
});
我得到的 javascript 错误通常是这样的: “未终止的正则表达式文字”
我还尝试删除 CFM 页面上的 SerialiseJson 和 javascript 中的 eval,这都会导致其他 js 错误。我还尝试删除周围的 html 以及将 .html() 更改为 .text(),删除 eval() 和所有这些的组合。没有运气。
请有人告诉我问题出在哪里。我知道我不会走得太远,因为它没有 html 也能工作。
谢谢
更新:我运行了 Coldfusion 通过 JSON 验证器生成的 JSON 输出,但它失败了,原因是(据我所知)双引号:
{""RETURNID"":"""",""DATA"":""<div>Hello World<\/div>"",""SUCCESS"":true,""ERRORS"":[]}
现在真不知道Coldfusion为什么要做双引号??有人有什么想法吗?
【问题讨论】:
-
不要使用
eval,使用真正的JSON解析器:github.com/douglascrockford/JSON-js -
如果您向我们展示一些 JSON 而不是一些生成 JSON 的 CF,将会有所帮助。
-
再次嗨 - 事实上,我确实在其他地方为我的 ajax 使用了 JSON 解析器,但我不确定在这种情况下你会如何使用它,因为没有真正的 ajax 发生。
标签: javascript jquery json coldfusion