jquery - 使用 AJAX 将表单数据发送到 Fw/1(Coldfusion) Controller 功能

标签 jquery ajax coldfusion cfml fw1

我是 FW1 的新手。我已经掌握了基础知识,但每当我尝试让它工作时,其他一些库都会很痛苦。很难弄清楚出了什么问题。

我想使用 AJAX 和 jQuery(submitForm.js) 将 newServer.cfm 中的表单数据发布到 main.cfc 中的 Controller 函数中。 Controller 函数将数据发送到服务(submit.cfc),服务将数据发送到 DAO(submit.cfc)以插入到数据库中。然后返回成功与否的状态。

文件夹结构

Folder Structure

submitForm.js

$(document).ready(function() {
    $("#submitForm").submit(function(){
        dataString = $("#submitForm").serialize();
        $.ajax({
        type: "POST",
        url: "index.cfm?action=main.submit",
        dataType: "json",
        data: dataString,
        success: function(response) {

            $("#result").html(response);

        },
                error: function(xhr, status, e) {
                    $("#result").html(status);
                }

        });

    });
});

ma​​in.cfc( Controller )

<cfcomponent accessors="true" output="false">

    <cfproperty name="submitService">

    <cfscript>

        function init( fw ) {
            variables.fw = fw;
            return this;
        }

            public function submit(rc){
            json = deserializeJson(rc);
            rc.status = submitService.submitForm(json);
            }

    </cfscript>

</cfcomponent>

submit.cfc(服务)

<cfcomponent accessors="true">

    <cfproperty name="submitDAO">

    <cffunction name="submitForm" returnType="boolean" access="public" output="false" hint="I return a list of blog entries">
        <cfargument name="json" type="struct" required="true" hint="" displayname="rc" />

        <cfset status = "">
        <cfset status = submitDAO.submitForm(json)>
        <cfreturn status>

    </cffunction>

</cfcomponent>

只是为了检查我正在从 DAO 返回一个 bool 值。

提交.cfc(DAO)

<cfcomponent accessors="true">

    <cffunction name="init" hint="I return an instance of myself">
        <cfreturn this>
    </cffunction>

    <cffunction name="submitForm" returntype="boolean" hint="I return a list of blog entries">
        <cfargument name="json" type="struct" required="true" hint="" displayname="rc" />
        <cfset var status = true>
        <cfreturn status>

    </cffunction>

</cfcomponent>

正在发布表单数据,但之后没有响应。 Firebug 在 jQuery 第 8630 行显示错误:

xhr.send( options.hasContent && options.data || null );

我尝试将ajax函数中的URL更改为“controllers/main.cfc?method=submit”,但仍然无济于事。

Firebug 控制台错误: enter image description here

最佳答案

我终于找到了解决办法。我的程序中有很多错误。我将其发布,以便如果有人遇到类似问题,他们可以引用代码。我将在评论中包含主要更改。

submitForm.js

$(document).ready(function() {
    $('#submitForm').submit(function(event){
        var dataString = $('#submitForm').serialize();
        $.ajax({
        type: 'POST',
        url: 'index.cfm?action=main.submit',
        data: dataString,
        dataType: 'json'
        })
        .done(function(response){

            console.log(response);

        })
        .fail(function(response) {

            console.log(response);

        });

    //I didnt prevent the default event from firing
    event.preventDefault();

    });
});

关于jquery - 使用 AJAX 将表单数据发送到 Fw/1(Coldfusion) Controller 功能,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34009758/

相关文章:

javascript - 在 Jquery AJAX 调用给出错误后重定向

javascript - 读取当前文本输入值后进行 Ajax 调用

coldfusion - 清除 Coldfusion 生产服务器中的缓存

javascript - 循环遍历不同的按钮文本.onClick

在 MVC 的 Global.asax 文件中使用 Application_PreRequestHandlerExecute 方法时,Jquery 和 css 在服务器上不起作用

javascript - javascript中如何从函数中知道对象?

sql - 将 Excel 电子表格导入 SQL 数据库 (Coldfusion 9)

jquery - 如何在同一元素上设置不同的工具提示和确认标题?

php - 通过 jQuery ajax 刷新 mysql 查询是否会使数据库崩溃?

arrays - 获取第二维数组长度