JSON response from my CFC is retunring HTML code

走远了吗. 提交于 2019-12-12 17:20:40

问题


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.

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

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>

Here is my 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>

Here is my Form:

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

回答1:


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.

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

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.

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.



来源:https://stackoverflow.com/questions/35707408/json-response-from-my-cfc-is-retunring-html-code

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!