我正在试图解决此问题。我已经实现了一个类似ColdFusion的Ajax文件上传,它使用一个隐藏的框架上传一个文件,然后抓取该框架返回的内容。它基于这篇文章:http://www.bennadel.com/blog/1244-ColdFusion-jQuery-And-AJAX-File-Upload-Demo.htm当html传入JSON数据包时出现Javascript错误
所以,一切工作都很好,除了当我发回数据中的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解析器:https://开头的github .com/douglascrockford/JSON-js – Quentin 2011-04-12 16:25:32
如果您向我们展示了一些JSON而不是生成JSON的某些CF,它会有所帮助。 – Quentin 2011-04-12 16:26:04
嗨,再次 - 我确实在其他地方使用JSON解析器来处理我的ajax,但我不知道在这种情况下如何使用它,因为没有真正的Ajax发生。 – Cheeky 2011-04-12 18:32:40