【问题标题】:Coldfusion Ajax-Request throws JSON.parse errorColdfusion Ajax-Request 抛出 JSON.parse 错误
【发布时间】:2018-08-03 09:50:03
【问题描述】:

我有一个非常简单的 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);
    });
}

【问题讨论】:

    标签: jquery json ajax coldfusion


    【解决方案1】:

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

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

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

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

    PS:使用varlocal 作用域初始化函数中的变量总是一个好主意。

    【讨论】:

    • @JenSeReal - 此外,为了获得更大的灵活性,请考虑在 URL 中指定 returnformat="json",而不是在函数中对其进行硬编码。
    猜你喜欢
    • 2021-04-10
    • 1970-01-01
    • 1970-01-01
    • 2012-01-18
    • 2020-02-04
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多