Coldfusion Ajax-请求抛出 JSON.parse 错误

Coldfusion Ajax-Request throws JSON.parse error

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

当我将代码复制到 .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 文件中创建 remote 函数时,CFC 路径开始像 Web 服务一样运行。当您像下面这样拨打电话时,

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

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

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

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