我是 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”,但仍然无济于事。