gpt4 book ai didi

jquery - Coldfusion 和 jquery ajax

转载 作者:行者123 更新时间:2023-12-01 03:44:25 28 4
gpt4 key购买 nike

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

ajax 调用:

$.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":"国际货币基金组织"}]

我的问题可能是什么?

另外,如果我在浏览器中直接访问我的 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>

最佳答案

我不确定这是否有帮助,但我正在处理 angularjs 的数据,并且正在查看 this post :讨论一下如何使用 CFC,无需自己创建 JSON(还演示了 CFMX 如何处理 json 调用)。

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

关于jquery - Coldfusion 和 jquery ajax,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15140423/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com