3

FW1初心者です。私は基本を持っていますが、それを機能させようとすると、他のライブラリが苦痛になります。何がうまくいかないのかを理解するのは難しいです。

AJAX と jQuery(submitForm.js) を使用して、newServer.cfm のフォーム データを main.cfc のコントローラー関数に投稿したいと考えています。コントローラー関数はデータをサービス (submit.cfc) に送信し、サービス (submit.cfc) はデータを DAO (submit.cfc) に送信してデータベースに挿入します。次に、成功したかどうかのステータスを返します。

フォルダ構造

フォルダ構造

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);
                }

        });

    });
});

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 からブール値を返しています。

submit.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

私は最終的に解決策を見つけました。私のプログラムには多くの間違いがありました。誰かが同様の問題に遭遇した場合にコードを参照できるように投稿します。コメントに主な変更点を含めます。

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();

    });
});
于 2015-12-01T02:56:27.163 に答える