且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

ColdFusion和jQuery的AJAX

更新时间:2023-09-11 23:20:28

我不知道这是否会帮助,但我的工作与angularjs数据的工作,并期待在这个帖子:会谈上使用CFC的瓦特/输出需要创建自己的JSON(还演示了如何CFMX处理JSON调用)有点

  

在ColdFusion MX的一个名为CFC直接返回WDDX。要制止这种,直接输出而不是[使用] cfreturn和追加cfabort即:

 < cfcontent复位=YES/>< CFOUTPUT> #Trim(EN code(qryExample))#< / CFOUTPUT>< cfabort /&GT ;
 

My cfc is working because I can invoke it and get the json string of data, however, my jquery ajax call gives me an error "SyntaxError: JSON.parse: unexpected end of data".

ajax call:

$.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);
    }
});

When I invoke the component in another cfm page my cfc returns the string: [{"client_id":58,"client_name":"Aimbridge"},{"client_id":104,"client_name":"IMF"}] .

What might my problem be?

also, if I go directly to my cfc in the browser http://domain.com/filepath/client.cfc?method=GetClientsByName&name=imI don't get errors but it is just a white screen. I don't know what I am supposed to be seeing when I go directly to it.

for the sake of all info here is 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>

Note: I'm using coldfusion7 so I cannot use returnformat='json'

application.cfc maybe something went awry here:

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

I'm not sure if this will help but I'm working on working with data for angularjs and was looking at this post: Talks a bit on using CFC's w/out the need to create the JSON yourself (also demonstrates how CFMX handled json calls).

In ColdFusion MX a cfc called directly returned wddx. To stop this, output directly instead of [using] cfreturn and append cfabort ie:

<cfcontent reset="yes" /><cfoutput>#Trim(encode(qryExample))#</cfoutput><cfabort />