我正在試圖解決此問題。我已經實現了一個類似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