Anuja
Anuja

Reputation: 1

coldfusion ajax json data return data not in proper format

I am trying to return an array of struct through ajax to provide autocomplete feature. I am trying to call getEmployeeName through ajax from the page where i want to populate the textbox.

The test.cfc is as follows:

<cfcomponent>
<cffunction name="getEmployeeName" access="remote" returnformat="JSON" >
    <cfargument name="term" type="string" required="yes">

    <cfset var returnArray=ArrayNew(1) />
    <cfset term=#Ucase(term)#>
    <cfquery name="getEmps" datasource="mines">
        ...
        <!--- my query ---!>
    </cfquery>

    <cfoutput>
    <cfloop query="getEmps">
        <cfset empStruct = StructNew() />
        <cfset empStruct["label"] = #emp_name# & " " &#desg_desc# & " " & #unit_name# />
        <cfset empStruct["value"] = #cpf_no#>

        <cfset ArrayAppend(returnArray,empStruct) />
    </cfloop>
    </cfoutput>
    <cfreturn #serializeJSON(returnArray)#>
</cffunction>

The ajax call is as follows:

$("#task_coordinator").autocomplete({
            source: function (request, response) {
                     $.ajax({
                             url: "test.cfc?method=getEmployeeName",
                             type: "GET",
                             data: request,
                             success: function (data) {



                                    console.log(data);
                                    console.log(data.array([label]));

                             },
                             error: function(jqXHR, extStatus,errorThrown){
                                alert(errorThrown);
                             }
                     });
            },
            select: function (event, ui) {

            }
            ,minLength:5
    });

I have tried calling the method directly through the link to see whether it is working fine or not. At that it is returning only json data and not this extra information. My output has extra script before json data:

<script type="text/javascript">/* <![CDATA[ */_cf_loadingtexthtml="<img alt=' ' src='/CFIDE/scripts/ajax/resources/cf/images/loading.gif'/>";
_cf_contextpath="";
_cf_ajaxscriptsrc="/CFIDE/scripts/ajax";
_cf_jsonprefix='//';
_cf_websocket_port=8577;
_cf_flash_policy_port=1243;
/* ]]> */</script><script type="text/javascript" 
src="/CFIDE/scripts/ajax/messages/cfmessage.js"></script>
<script type="text/javascript" src="/CFIDE/scripts/ajax/package/cfajax.js">
</script>
<script type="text/javascript" src="/CFIDE/scripts/cfform.js"></script>
<script type="text/javascript" src="/CFIDE/scripts/masks.js"></script>
<script type="text/javascript" src="/CFIDE/scripts/cfformhistory.js">
</script>

<script type="text/javascript">/* <![CDATA[ */
    ColdFusion.Ajax.importTag('CFAJAXPROXY');
/* ]]> */</script>

<script type="text/javascript">/* <![CDATA[ */
    ColdFusion.Ajax.importTag('CFFORM');
/* ]]> */</script>
<script type="text/javascript">
<!--
    _CF_checkCFForm_1 = function(_CF_this)
    {
        //reset on submit
        _CF_error_exists = false;
        _CF_error_messages = new Array();
        _CF_error_fields = new Object();
        _CF_FirstErrorField = null;


        //display error messages and return success
        if( _CF_error_exists )
        {
           if( _CF_error_messages.length > 0 )
           {
                // show alert() message
                _CF_onErrorAlert(_CF_error_messages);
                // set focus to first form error, if the field supports js focus().
                if( _CF_this[_CF_FirstErrorField].type == "text" )
                { _CF_this[_CF_FirstErrorField].focus(); }

            }
            return false;
        }else {
            return true;
        }
    }
//-->
</script>
[{"value":47455,"label":"BANTHIA ANUJA RAVINDRAKUMAR GRADUATE EXECUTIVE TRAINEE(E2) CENTRAL ESTABLISHMENT"}]   

Can somebody tell me why this extra script is coming. Thanks in advance!

Upvotes: 0

Views: 244

Answers (1)

Chris Geirman
Chris Geirman

Reputation: 9684

In your ColdFusion Administrator, select Debugging & Logging > Debugger Settings. You should find some settings in there to help debug AJAX requests. Try turning that off if it's checked.

enter image description here

Here's some info that might help. http://help.adobe.com/en_US/ColdFusion/9.0/Developing/WSc3ff6d0ea77859461172e0811cbec10e40-8000.html

Upvotes: 0

Related Questions