我的CFC的JSON响应是retunring HTML代码 [英] JSON response from my CFC is retunring HTML code

查看:157
本文介绍了我的CFC的JSON响应是retunring HTML代码的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这里有一个奇怪的问题。我使用jquery调用CFC并返回一个字符串。我然后试图填充一个表单字段与该字符串。

I have a strange issue here. I am making a call to a CFC using jquery and returning a string. I am then trying to populate a form field with that string. For some reason, my response is including HTML code along with the query results.

以下是JSON响应在控制台中的显示效果:

Here is how the JSON response looks in the console:

> 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:

Here is my 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 - 它可能是设置页眉/页脚值。查找可能提示您的onrequestend()和Onrequest()或onrequeststart()。

You should look at your Application.cfc file - or Application.cfm - it is probably setting up header/footer values. Look for "onrequestend()" and "Onrequest()" or "onrequeststart()" - that might clue you in.

您可能还有一个自定义标签类型方法

You might also have a custom tag type approach in there -- something that wraps every request.

您的 returnType将需要设置为json ,否则您只需要输入274.00即可没有json包装。

Your returnType will need to be set to json - otherwise you just get "274.00" with no json wrappers.

要修复HTML,您必须检查CFC,而不是运行相关函数。或者,您可以在CFC的文件夹中使用单独的Application.cfc,完全排除这些功能。当你直接从代码(如createobject())中访问你的CFC时,这样一个单独的Application.cfc不起作用 - 但是当它们是你的示例中的请求的基本模板时就可以播放。

To fix the HTML you have check for a CFC and not run the function in question. Alternately you can use a separate Application.cfc in the folder of your CFCs that excludes those functions altogether. Such a separate Application.cfc is not in play when you access your CFC's directly from code (as in createobject()) - but comes in to play when they are the base template for a request as in your example.

记住,你可以使用method = getTotal&等等 - 无需在控制台中解决此问题,直到您在浏览器中获得正确的结果。

Remember that you can test right from the browser using method=getTotal& etc. - no need to troubleshoot this in the console until you get the results correct in the browser.

这篇关于我的CFC的JSON响应是retunring HTML代码的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆