2

我的 cfc 正在工作,因为我可以调用它并获取数据的 json 字符串,但是,我的 jquery ajax 调用给了我一个错误“SyntaxError: JSON.parse: unexpected end of data”。

阿贾克斯调用:

$.ajax({
    type: "get",
    url: "/surveymanagement/admin/client.cfc",
    dataType: "json",
    data: {
        method: "GetClientsByName",
        name: "im"
    },
    success: function(data){
        $("#here").html(data);
    },
    error: function(a,b,c){
        $("#here").html(a.responseText + c);
    }
});

当我在另一个 cfm 页面中调用该组件时,我的 cfc 返回字符串:[{"client_id":58,"client_name":"Aimbridge"},{"client_id":104,"client_name":"IMF"}]

我的问题可能是什么?

另外,如果我直接在浏览器中访问我的 cfc,http://domain.com/filepath/client.cfc?method=GetClientsByName&name=im我不会收到错误,但它只是一个白屏。我不知道当我直接进入它时我应该看到什么。

为了所有信息,这里是client.cfc:

<cfcomponent>
<cfsetting showdebugoutput="no">
<cffunction name="GetClientsByName"
    access="remote"
    returntype="string" 
    hint="get clients from search term">

    <cfargument name="name" type="string" required="yes">

    <cfset var util = createObject("component", "/surveymanagement/JSONUtil")>
    <cfset var results = arrayNew(1)>
    <cfset var elem = "">

    <cfquery name="GetClientsByName" datasource="#application.dsn#">
        SELECT client_id, client_name
        FROM Clients
        WHERE client_name LIKE <cfqueryparam cfsqltype="cf_sql_varchar" value="%#arguments.name#%">
    </cfquery>

    <cfloop query="GetClientsByName">
        <cfset elem = structNew()>
        <cfset elem["client_id"] = GetClientsByName.client_id>
        <cfset elem["client_name"] = GetClientsByName.client_name>
        <cfset arrayAppend(results, elem)>
    </cfloop>

    <cfcontent type="application/json" reset="true">
    <cfreturn util.serializeJSON(results)>
</cffunction>
</cfcomponent>

注意:我正在使用coldfusion7所以我不能使用returnformat='json'

application.cfc 也许这里出了点问题:

<cfcomponent 
displayname="Application"
output="true"
hint="Handle the Application">

<cfset THIS.Name = "SurveyManagement" />
<cfset THIS.ApplicationTimeout = CreateTimeSpan(0,1,0,0) />
<cfset THIS.SessionManagement = true />
<cfset THIS.SetClientCookies = false />

<cffunction
    name="OnApplicationStart"
    access="public"
    returntype="boolean"
    output="false"
    hint="Fires when the application is first created.">

    <cfreturn true />
</cffunction>


<cffunction
    name="OnSessionStart"
    access="public"
    returntype="void"
    output="false"
    hint="Fires when the session is first created.">

    <cfreturn />
</cffunction>


<cffunction
    name="OnRequestStart"
    access="public"
    returntype="boolean"
    output="false"
    hint="Fires at first part of page processing.">

    <cfargument
        name="TargetPage"
        type="string"
        required="true"
        />
    <cfparam name="variables.this_query_string" default="">
    <cfif cgi.QUERY_STRING neq "">
        <cfset variables.this_query_string="?#cgi.QUERY_STRING#">
    </cfif>
    <cfparam name="server.ThisServer" default="www4.mydomain.com">
    <cfif cgi.SERVER_PORT neq "443" or cgi.HTTP_HOST eq "www.mydomain.com">
        <cflocation url="https://#server.ThisServer##cgi.PATH_INFO##variables.this_query_string#" addtoken="no">
    </cfif>     

    <cfparam name="form.fieldnames" default="">
    <cfloop list="#form.fieldnames#" index="i">
        <cfif Evaluate(i) neq "">
            <cfset form_value_temp=Evaluate(i)>
            <cfset form_value_temp=replace(form_value_temp,"<", "&lt;", "all")>
            <cfset form_value_temp=replace(form_value_temp,">", "&gt;", "all")>
            <cfset "form.#i#"=form_value_temp>
        </cfif>
    </cfloop>    
    <cfreturn true />
</cffunction>


<cffunction
    name="OnRequest"
    access="public"
    returntype="void"
    output="true"
    hint="Fires after pre page processing is complete.">

    <cfargument
        name="TargetPage"
        type="string"
        required="true"
        />

    <cfset application.dsn="SurveyManagement">
    <cfset application.title="Survey Management Site">
    <cfset application.directory="surveymanagement">
    <cfset application.cfc_data="surveymanagement">

    <cfinclude template="#ARGUMENTS.TargetPage#" />

    <cfreturn />
</cffunction>


<cffunction
    name="OnRequestEnd"
    access="public"
    returntype="void"
    output="true"
    hint="Fires after the page processing is complete.">

    <cfreturn />
</cffunction>


<cffunction
    name="OnSessionEnd"
    access="public"
    returntype="void"
    output="false"
    hint="Fires when the session is terminated.">

    <cfargument
        name="SessionScope"
        type="struct"
        required="true"
        />

    <cfargument
        name="ApplicationScope"
        type="struct"
        required="false"
        default="#StructNew()#"
        /> 

    <cfreturn />
</cffunction>


<cffunction
    name="OnApplicationEnd"
    access="public"
    returntype="void"
    output="false"
    hint="Fires when the application is terminated.">

    <cfargument
        name="ApplicationScope"
        type="struct"
        required="false"
        default="#StructNew()#"
        />

    <cfreturn />
</cffunction>


<cffunction
    name="OnError"
    access="public"
    returntype="void"
    output="true"
    hint="Fires when an exception occures that is not caught by a try/catch.">

    <cfargument name="Except" required=true/>
    <p>AN UNEXPECTED ERROR HAS OCCURRED</p>
    <p>Please consult your suggested technical support contact for assistance.</p>
    <cfif findnocase("ip address here",cgi.REMOTE_ADDR) or findnocase("ip address here",cgi.REMOTE_ADDR)>
        <cfdump var="#Except.RootCause#">
    </cfif>
    <cfreturn />
</cffunction>

4

1 回答 1

3

我不确定这是否会有所帮助,但我正在处理 angularjs 的数据并正在查看这篇文章:Talks a bit on using CFC's w/out the need to create the JSON (还演示了 CFMX 如何处理 json来电)。

在 ColdFusion MX 中,一个名为直接返回 wddx 的 cfc。要停止这种情况,请直接输出而不是 [使用] cfreturn 并附加 cfabort 即:

<cfcontent reset="yes" /><cfoutput>#Trim(encode(qryExample))#</cfoutput><cfabort />
于 2013-02-28T16:39:40.667 回答