$.ajax 调用 ColdFusion 组件输出文本而不是 HTML
如果这个问题已经得到回答,我提前道歉。
我的目标是创建一个分页方案,将变量发送到 ColdFusion 函数中的查询,该函数获取 X 个元素并在我的网页上显示这些记录而不刷新它。因此,为了尝试测试抓取 HTML 内容并将其显示到屏幕而不刷新的基本功能,我尝试对 ColdFusion 组件执行一个简单的 AJAX 调用,让它返回一个包含 HTML 内容的变量,并显示格式化的 HTML在我的网页上的 div 标签内。这是基于我之前在网上看到的一个示例,其中它使用了 cfajaxproxy,虽然这可能是我的问题,但令我惊讶的是它根本没有做它应该做的事情。
我有一个名为 testGeneration.cfm 的文件,代码如下:
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
<title>Untitled Document</title>
<!-- call the jQuery library -->
<script src="jquery.min.js" type="text/javascript"></script>
<script type="text/javascript">
$(document).ready(function() {
$("#loadLink").click(function(e) {
e.preventDefault();
$.ajax({
type: "GET",
url: "generateInfo.cfc?method=createHTML",
dataType: "html",
success: function(message) {
$("#mydiv").html(message);
}
});
});
});
</script>
</head>
<body>
<a href="" id="loadLink">Load Query</a>
<!-- empty div to load dynamcically generated table into -->
<div id="mydiv"></div>
</body>
正如您所看到的,它使用 CreateHTML 方法调用组件GenerateInfo。以下是generateInfo.cfc的代码:
<cfcomponent displayname="Generate HTML" output="false">
<cffunction name="createHTML" displayname="Create HTML" description="Creates HTML to output with jQuery." access="remote" output="false" returntype="string">
<!--- SET VARIABLE TO RETURN --->
<cfset VARIABLES.html = "">
<!--- SAVE CONTENT --->
<cfsavecontent variable="VARIABLES.html">
<strong>hello world</strong>
</cfsavecontent>
<!--- RETURN SAVED CONTENT --->
<cfreturn VARIABLES.html>
</cffunction>
现在,当我单击超链接时,它应该将内容显示为
hello world
相反,它将内容显示为
hello world
我做错了什么?如何让它处理作为 Coldfusion 组件函数中的返回变量发回的 HTML?
托尼
I apologize in advance if this was already answered.
My goal is to create a pagination scheme that sends variables to a query in a ColdFusion function that grabs X number of elements and displays those records on my web page without refreshing it. So in an attempt to test the basic function of grabbing HTML content and displaying it to the screen without refreshing, I am trying to execute a simple AJAX call to a ColdFusion component, having it return a variable with HTML content, and display the formatted HTML inside a div tag on my web page. This is based on a previous example I saw on the web where it used a cfajaxproxy instead, and while that could be my problem, I'm amazed that it's simply not doing what it's supposed to.
I have a file called testGeneration.cfm and here is the code:
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
<title>Untitled Document</title>
<!-- call the jQuery library -->
<script src="jquery.min.js" type="text/javascript"></script>
<script type="text/javascript">
$(document).ready(function() {
$("#loadLink").click(function(e) {
e.preventDefault();
$.ajax({
type: "GET",
url: "generateInfo.cfc?method=createHTML",
dataType: "html",
success: function(message) {
$("#mydiv").html(message);
}
});
});
});
</script>
</head>
<body>
<a href="" id="loadLink">Load Query</a>
<!-- empty div to load dynamcically generated table into -->
<div id="mydiv"></div>
</body>
As you can see it calls the component GenerateInfo with the method CreateHTML. Here is the code for generateInfo.cfc:
<cfcomponent displayname="Generate HTML" output="false">
<cffunction name="createHTML" displayname="Create HTML" description="Creates HTML to output with jQuery." access="remote" output="false" returntype="string">
<!--- SET VARIABLE TO RETURN --->
<cfset VARIABLES.html = "">
<!--- SAVE CONTENT --->
<cfsavecontent variable="VARIABLES.html">
<strong>hello world</strong>
</cfsavecontent>
<!--- RETURN SAVED CONTENT --->
<cfreturn VARIABLES.html>
</cffunction>
Now, when I click the hyperlink, it should display the content as
hello world
Instead, it displays the content as
<strong>hello world</strong>
What am I doing wrong? How do I get it to process the HTML being sent back as a return variable in a coldfusion component function?
Tony
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
请尝试将您的 $.ajax 调用更改为此,看看是否可以解决您的问题:
编辑
根据您的评论,尝试将其添加到 cffunction 标记中:
Please try changing your $.ajax call to this and see if it resolves your problem:
edit
Based on your comment, try adding this to the cffunction tag:
我使用此处显示的答案来帮助我解决问题,因为答案已经被标记,而且我也遇到了同样的问题。但标记的答案没有帮助;造成了几个小时的沮丧和叹息。这是因为我在 cfcontentsave 标记内有 cfoutput。
下面的答案是另一种选择:
cfsavecontent 是让我搞砸的事情。如果我想在该区域内执行 cfoutput,例如:
上面的方法将不起作用。
解决方法:
注意:不需要 returnformat 属性。 cfreturn 也不是。 ,您仍然可以使用
此外
I was using the answer displayed here as a way to help me out since the answer was already marked and since I had the same exact problem. But the answer that was marked did not help; causing a few hours of frustration and sighs. This is because I had cfoutput within the cfcontentsave tag.
The answer below is an alternative:
The cfsavecontent was the thing that was messing things up for me. If I wanted to do a cfoutput within that area like:
The above would not work.
The workaround:
Note: the returnformat attribute is not needed. Neither is a cfreturn. In addition, you could still use
instead of the