ColdFusion和jQuery的AJAX

ColdFusion和jQuery的AJAX

本文介绍了ColdFusion和jQuery的AJAX的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的CFC的工作,因为我可以调用它,并获取数据的JSON字符串,但是,我的jQuery ajax调用给我一个错误语法错误:JSON.parse:数据意外结束。

Ajax调用:

  $。阿贾克斯({
    键入:搞定,
    网址:/surveymanagement/admin/client.cfc
    数据类型:JSON,
    数据: {
        方法:GetClientsByName
        名称:IM
    },
    成功:功能(数据){
        $(#这里)的HTML(数据)。
    },
    错误:功能(A,B,C){
        $(#这里)的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 =无>
< cffunction名=GetClientsByName
    访问=远程
    返回类型=字符串
    提示=得到的搜索字词客户>

    < cfargument名称=名称TYPE =字符串要求=是>

    < CFSET VAR UTIL =的CreateObject(组件,/ surveymanagement / JSONUtil)>
    &所述; CFSET变种结果= arrayNew(1)>
    < CFSET VAR的elem =>

    < CFQUERY NAME =GetClientsByName数据源=#application.dsn#>
        SELECT CLIENT_ID,CLIENT_NAME
        来自客户端
        其中,客户LIKE< cfqueryparam cfsqltype =cf_sql_varchar值=%#arguments.name#%>
    < / CFQUERY>

    < CFLOOP查询=GetClientsByName>
        < CFSET ELEM = structNew()>
        < CFSET ELEM [CLIENT_ID] = GetClientsByName.client_id>
        < CFSET ELEM [CLIENT_NAME] = GetClientsByName.client_name>
        < CFSET arrayAppend(结果,ELEM)>
    < / CFLOOP>

    < cfcontent类型=应用/ JSON的复位=真正的>
    < cfreturn util.serializeJSON(结果)>
< / cffunction>
< / CFCOMPONENT>
 

注:我使用coldfusion7所以我不能使用 returnformat ='json的

的Application.cfc或许真的走到歪在这里:

 < CFCOMPONENT
显示名称=应用程序
输出=真
提示=处理应用程序>

< CFSET THIS.Name =SurveyManagement/>
< CFSET THIS.ApplicationTimeout = CreateTimeSpan(0,1,0,0)/>
< CFSET THIS.SessionManagement = TRUE />
< CFSET THIS.SetClientCookies = FALSE />

< cffunction
    NAME =OnApplicationStart
    访问=公共
    返回类型=布尔
    输出=假
    当第一次创建时触发的应用程序。暗示=>

    < cfreturn真/>
< / cffunction>


< cffunction
    NAME =OnSessionStart
    访问=公共
    返回类型=无效
    输出=假
    当第一次创建时触发的会话。暗示=>

    < cfreturn />
< / cffunction>


< cffunction
    NAME =OnRequestStart
    访问=公共
    返回类型=布尔
    输出=假
    提示=火灾在页面处理的第一部分。>

    < cfargument
        NAME =TargetPage
        TYPE =字符串
        所需=真
        />
    < cfparam名=variables.this_query_string默认值=>
    < CFIF cgi.QUERY_STRING NEQ>
        <?#cgi.QUERY_STRING#CFSET variables.this_query_string =>
    < / CFIF>
    < cfparam名=server.ThisServer默认值=www4.mydomain.com>
    < CFIF cgi.SERVER_PORT NEQ443或cgi.HTTP_HOST情商www.mydomain.com>
        < cflocation URL =HTTPS://#server.ThisServer##cgi.PATH_INFO##variables.this_query_string#addtoken =无>
    < / CFIF>

    < cfparam名=form.fieldnames默认值=>
    < CFLOOP列表=#form.fieldnames#指数=我>
        < CFIF评估(我)NEQ>
            < CFSET form_value_temp =评估(I)GT;
            &所述; CFSET form_value_temp =取代(form_value_temp,&所述;,&安培;其中;,所有)>
            &所述; CFSET form_value_temp =取代(form_value_temp,>中,与& gt;中,所有)>
            < CFSET的形式#I#= form_value_temp>
        < / CFIF>
    < / CFLOOP>
    < cfreturn真/>
< / cffunction>


< cffunction
    NAME =OnRequest
    访问=公共
    返回类型=无效
    输出=真
    提示=之后pre页面处理火灾就完成了​​。>

    < cfargument
        NAME =TargetPage
        TYPE =字符串
        所需=真
        />

    < CFSET application.dsn =SurveyManagement>
    < CFSET application.title =测量管理站点>
    < CFSET application.directory =surveymanagement>
    < CFSET application.cfc_data =surveymanagement>

    < cfinclude模板=#ARGUMENTS.TargetPage#/>

    < cfreturn />
< / cffunction>


< cffunction
    NAME =OnRequestEnd
    访问=公共
    返回类型=无效
    输出=真
    提示=页面处理火灾后完成。>

    < cfreturn />
< / cffunction>


< cffunction
    NAME =OnSessionEnd
    访问=公共
    返回类型=无效
    输出=假
    提示=火灾时,会话将被终止。>

    < cfargument
        NAME =SessionScope
        TYPE =结构
        所需=真
        />

    < cfargument
        NAME =ApplicationScope
        TYPE =结构
        所需=假
        默认值=#StructNew()#
        />

    < cfreturn />
< / cffunction>


< cffunction
    NAME =OnApplicationEnd
    访问=公共
    返回类型=无效
    输出=假
    提示=火灾时,该应用程序将被终止。>

    < cfargument
        NAME =ApplicationScope
        TYPE =结构
        所需=假
        默认值=#StructNew()#
        />

    < cfreturn />
< / cffunction>


< cffunction
    NAME =的OnError
    访问=公共
    返回类型=无效
    输出=真
    提示=时触发异常occures未捕获一个try / catch。>

    < cfargument名=除非需要=真/>
    < P>一种意外出错< / P>
    < P>请咨询您的建议技术支持联系以获得帮助和LT; / P>
    < CFIF findnocase(这里的IP地址,cgi.REMOTE_ADDR)或findnocase(这里的IP地址,cgi.REMOTE_ADDR)>
        &所述; cfdump变种=#Except.RootCause#>
    < / CFIF>
    < cfreturn />
< / cffunction>
 

解决方案

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

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

这篇关于ColdFusion和jQuery的AJAX的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-27 13:38