3

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

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

文件夹结构

文件夹结构

提交表单.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);
                }

        });

    });
});

main.cfc(控制器)

<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 返回一个布尔值。

提交.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 控制台错误: 在此处输入图像描述

4

1 回答 1

1

我终于找到了解决方案。我的程序有很多错误。我会发布它,以便如果有人遇到类似问题,他们可以参考代码。我将在评论中包括主要更改。

提交表单.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();

    });
});
于 2015-12-01T02:56:27.163 回答