JSON 来自我的 CFC 的响应正在返回 HTML 代码
JSON response from my CFC is retunring HTML code
我这里有一个奇怪的问题。我正在使用 jquery 调用 CFC 并返回一个字符串。然后我试图用该字符串填充表单字段。出于某种原因,我的回复包括 HTML 代码和查询结果。
下面是 JSON 响应在控制台中的样子:
> Gary Turner check_out.cfm:146 Successfully ran JSON, now changing
> input value check_out.cfm:149 <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML
> 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <head>
> <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
>
> </head> <body>
>
>
>
>
>
> </body>
> 274.00
这是我的 JQUERY:
<!---Populate Grand Total --->
<script>
function PopulateGrandTotal(){
// Populate the customer alert DIV based on the customer selection
console.log( $("#customer_checkout>option:selected").attr("Value") );
$.ajax({
url:'cfcs/grand_totalDIV.cfc?method=getTotal&returnformat=json',
//dataType: 'text',
data: { customer_checkout: $("#customer_checkout>option:selected").attr("Value") },
success: function(response) {
console.log('Successfully ran JSON, now changing input value');
$("#grand_total_due").val( response );
console.log(response);
},
error: function(response){
console.log('Error');
}
});
}
</script>
这是我的 CFC:
<cffunction name="getTotal" access="remote" returntype="string">
<cfargument name="customer_checkout" type="any" required="true">
<!--- localize function variables --->
<cfset var dataDetail = "">
<cfquery name="dataDetail" datasource="#datasource#" >
select grand_total
from service_ticket
where company_name = <cfqueryparam value="#ARGUMENTS.customer_checkout#" cfsqltype="cf_sql_varchar">
</cfquery>
<cfoutput query="dataDetail">
<cfreturn dataDetail.grand_total>
</cfoutput>
</cffunction></cfcomponent>
这是我的表格:
<cfform id="form" name="form" method="post" action="signature_popup.cfm" >
<br><br>
<div align="center"><cfselect class="required" queryPosition="below" query="get_ticket" display="company_name" name="customer_checkout" id="customer_checkout" tabindex="0" onchange="PopulateGrandTotal();" ><option>---Make A Selection---</option></cfselect>
<br><br>
<div id="grant_totalDIV" >
<h2><label for="grand_total_due">Total Amount Due:</label><input type="text" name="grand_total_due" id="grand_total_due"> </h2>
</div>
<br>
您应该查看 Application.cfc 文件 - 或 Application.cfm - 它可能正在设置 header/footer 值。寻找 "onrequestend()" 和 "Onrequest()" 或 "onrequeststart()" - 这可能会为您提供线索。
您可能还有一个自定义标记类型的方法 -- 包装每个请求的方法。
您的 returnType 需要设置为 json - 否则您只会得到没有 json 包装器的“274.00”。
要修复 HTML,您需要检查 CFC 而不是 运行 有问题的函数。或者,您可以在完全排除这些功能的 CFC 文件夹中使用单独的 Application.cfc。当您直接从代码访问您的 CFC(如在 createobject() 中)时,这样一个单独的 Application.cfc 不会发挥作用 - 但是当它们是您的示例中的请求的基本模板时发挥作用。
请记住,您可以使用 method=getTotal& 等直接从浏览器进行测试 - 在浏览器中获得正确结果之前,无需在控制台中对此进行故障排除。
我这里有一个奇怪的问题。我正在使用 jquery 调用 CFC 并返回一个字符串。然后我试图用该字符串填充表单字段。出于某种原因,我的回复包括 HTML 代码和查询结果。
下面是 JSON 响应在控制台中的样子:
> Gary Turner check_out.cfm:146 Successfully ran JSON, now changing
> input value check_out.cfm:149 <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML
> 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <head>
> <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
>
> </head> <body>
>
>
>
>
>
> </body>
> 274.00
这是我的 JQUERY:
<!---Populate Grand Total --->
<script>
function PopulateGrandTotal(){
// Populate the customer alert DIV based on the customer selection
console.log( $("#customer_checkout>option:selected").attr("Value") );
$.ajax({
url:'cfcs/grand_totalDIV.cfc?method=getTotal&returnformat=json',
//dataType: 'text',
data: { customer_checkout: $("#customer_checkout>option:selected").attr("Value") },
success: function(response) {
console.log('Successfully ran JSON, now changing input value');
$("#grand_total_due").val( response );
console.log(response);
},
error: function(response){
console.log('Error');
}
});
}
</script>
这是我的 CFC:
<cffunction name="getTotal" access="remote" returntype="string">
<cfargument name="customer_checkout" type="any" required="true">
<!--- localize function variables --->
<cfset var dataDetail = "">
<cfquery name="dataDetail" datasource="#datasource#" >
select grand_total
from service_ticket
where company_name = <cfqueryparam value="#ARGUMENTS.customer_checkout#" cfsqltype="cf_sql_varchar">
</cfquery>
<cfoutput query="dataDetail">
<cfreturn dataDetail.grand_total>
</cfoutput>
</cffunction></cfcomponent>
这是我的表格:
<cfform id="form" name="form" method="post" action="signature_popup.cfm" >
<br><br>
<div align="center"><cfselect class="required" queryPosition="below" query="get_ticket" display="company_name" name="customer_checkout" id="customer_checkout" tabindex="0" onchange="PopulateGrandTotal();" ><option>---Make A Selection---</option></cfselect>
<br><br>
<div id="grant_totalDIV" >
<h2><label for="grand_total_due">Total Amount Due:</label><input type="text" name="grand_total_due" id="grand_total_due"> </h2>
</div>
<br>
您应该查看 Application.cfc 文件 - 或 Application.cfm - 它可能正在设置 header/footer 值。寻找 "onrequestend()" 和 "Onrequest()" 或 "onrequeststart()" - 这可能会为您提供线索。
您可能还有一个自定义标记类型的方法 -- 包装每个请求的方法。
您的 returnType 需要设置为 json - 否则您只会得到没有 json 包装器的“274.00”。
要修复 HTML,您需要检查 CFC 而不是 运行 有问题的函数。或者,您可以在完全排除这些功能的 CFC 文件夹中使用单独的 Application.cfc。当您直接从代码访问您的 CFC(如在 createobject() 中)时,这样一个单独的 Application.cfc 不会发挥作用 - 但是当它们是您的示例中的请求的基本模板时发挥作用。
请记住,您可以使用 method=getTotal& 等直接从浏览器进行测试 - 在浏览器中获得正确结果之前,无需在控制台中对此进行故障排除。