gpt4 book ai didi

jquery - Coldfusion Ajax-Request 抛出 JSON.parse 错误

转载 作者:行者123 更新时间:2023-12-01 03:07:02 26 4
gpt4 key购买 nike

我有一个非常简单的 Ajax 请求。它只是从数据库请求一些数据。问题是我总是收到 JSON.parse 错误(第一行,第一列)。 Coldfusion 函数输出的 JSON 有效且正确。

当我将代码复制到 .cfc 文件时,一切正常。这是为什么?

冷融合:

<cffunction name="getAllDatatypes" access="remote" returntype="query" returnformat="json">

<cfquery name="getAllDatatypes" datasource="#DSN#">
SELECT
id_datatype,
name
FROM
datatype
</cfquery>

<cfreturn getAllDatatypes>

</cffunction>

JQuery:

function getAllDatatypes() {
$.ajax({
url: "getData.cfm",
type: "post",
dataType: "json",
data: {method: "getAllDatatypes"}
}).done(function(result) {
console.log(result);
}).fail(function(error1, error2, error3) {
console.log(error1 + " " + error2 + " " + error3);
});
}

最佳答案

当您在 .cfc 文件中创建远程函数时,CFC 路径开始像 Web 服务一样运行。当您调用如下电话时,

http://localhost/components/testcomp.cfc?method=getAllDatatypes

ColdFusion 知道您正在尝试调用 components/testcomp.cfc 组件中的远程函数 getAllDatatypes,并且如果远程函数 getAllDatatypes 则调用该函数找到 code> 并以 plaintextxmljson 形式返回响应。

另一方面。任何对 .cfm 文件的调用,都不存在这种情况。即使您已在文件中创建了函数,ColdFusion 也不会查找 .cfm 文件中的远程方法调用。

PS:使用 var 或使用 local 作用域在函数内部初始化变量始终是一个好主意。

关于jquery - Coldfusion Ajax-Request 抛出 JSON.parse 错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51669924/

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