3

我之前使用过这个确切的代码 - 使用 PHP 作为处理页面,但现在在冷融合中(无论我使用 cfc 还是只使用 cfm 来处理),jQuery 似乎没有传递表单对象 - 好吧......如果我查看了萤火虫响应,“发布”选项卡确实显示了所有字段及其值.. application/x-www-form-urlencoded 但是在 URL 属性中负责的页面没有得到它.. 并且(在示例 delow)控制台日志(数据)显示一个空字符串。

这就是我所拥有的......

$( '#create_client' ).on( 'click', function( event ) {

    //form validation
    var bValid = true;

    if ( bValid ) {
        // AJAX Post data
        $.ajax({
            type: 'POST',
            dataType: 'html',
            cache: false,
            url: '/cfc/clent.cfc?method=put',
            data: $( '#client-form' ).serialize(),
            success: function( data ){
                console.log(data);
                loadClientTable();
            },
            error: function(){
                // tell user there was a problem
                $( '#clients-message' ).html( '<p>There was a problem submitting your request... Doh!</p>' );
            }
        });
    }

}

这是表格

<form name="client-form" id="client-form" class="singleLineForm" >
<label for="firstname">First Name: </label>
<input type="text" name="firstname" id="firstname" value="" maxlength="15" class="text ui-widget-content ui-corner-all" />
<label for="lastname">Last Name: </label>
<input type="text" name="lastname" id="lastname" value="" maxlength="15" class="text ui-widget-content ui-corner-all" />
<label for="email">Email: </label>
<input type="text" name="email" id="email" value="" maxlength="50" class="text ui-widget-content ui-corner-all" />
<label for="password">Password: </label>
<input type="text" name="password" id="password" value="" maxlength="20" class="text ui-widget-content ui-corner-all" />
<label for="isActive">Active? </label>
<input type="checkbox" name="isActive" id="isActive" value="1" class="checkbox ui-widget-content ui-corner-all" />
<input type="button" name="create_client" id="create_client" value="Create Client" class="button ui-widget-content ui-corner-all" />
</form>

放入 cfc 看起来像这样(现在)

<cffunction name="put" access="public" returntype="structure" hint="PUT method, handles data transfering." >
    <cfargument name="email" type="string" required="yes" >
    <cfargument name="password" type="string" required="yes" >
    <cfargument name="firstname" type="string" required="yes" >
    <cfargument name="lastname" type="string" required="yes" >
    <cfargument name="isActive" type="numeric" required="yes" >

    <cfset _return = '' />

    <cfquery name="insertClient" datasource="#application.DSNwrite#" >
        INSERT INTO clients
        ( 
            email
            ,password
            ,firstname
            ,lastname
            ,isActive
        )
        VALUES
        ( 
            <cfqueryparam cfsqltype="cf_sql_varchar" value="#trim( arguments.email )#" />
            ,<cfqueryparam cfsqltype="cf_sql_varchar" value="#trim( arguments.password )#" />
            ,<cfqueryparam cfsqltype="cf_sql_varchar" value="#trim( arguments.firstname )#" />
            ,<cfqueryparam cfsqltype="cf_sql_varchar" value="#trim( arguments.lastname )#" />
            ,<cfqueryparam cfsqltype="cf_sql_bit" value="#val( arguments.isActive )#" />
        )
    </cfquery>

    <cfreturn _return />
</cffunction>
4

2 回答 2

5

您的 CFC 方法返回一个空字符串。

你有这个:

<cfset _return = '' />

在方法的最后,你有这个:

<cfreturn _return />

但是,您永远不会设置_return为不同的值。要测试这是问题所在,请尝试以下操作:

 <cfset _return = 'moo' />

如果您返回“moo”,则一切都按预期工作。

于 2013-04-14T13:07:37.907 回答
0

You can do like below to pass form data in ajax call.

var formData = $('#client-form').serialize();
$.ajax({
    url: '/cfc/clent.cfc?method=put&' + formData,
    type: 'POST',
    data:{
    },
    success: function(data){},
    error: function(data){},
})
于 2017-05-10T05:02:29.013 回答