对ColdFusion组件的Ajax jQuery调用
问题描述:
我是jQuery的新手,并且正在尝试创建一个登录页面,该页面将对CFC执行Ajax调用,该登录页面简单地返回true或false,表示登录是否成功。我的电话正确地使用了我的论点向CFC发出了通知,但返回的是问题。如果我在jQuery中设置我的数据类型为“html”,我会看到我的整个页面在html中的副本以及我正在寻找的“真实”值。但如果我尝试将其设置为“json”,则不会发生任何事情。我在ColdFusion 9,jQuery 1.6.2上。对ColdFusion组件的Ajax jQuery调用
我jQuery的功能:
$(function() {
$('.error').hide();
$(".button").click(function() {
// validate form here
$('.error').hide();
var name = $("input#username").val();
var pass = $("input#password").val();
if (name == "") {
$("label#username_error").show();
$("input#username").focus();
return false;
}
else if (pass == "") {
$("label#password_error").show();
$("input#password").focus();
return false;
}
else {
var bodyContent = $.ajax({
url: "cfc/Login.cfc?method=tryLogin&returnFormat=JSON",
global: false,
type: "POST",
data: {username:name,password:pass},
dataType: "json",
async:false,
success: function(msg){
alert(msg);
}
}
).responseText;
}
});
});
我CFC代码,很简单,我只是试图让这个正确回到现在:
<cfcomponent name="Login" output="false">
<cffunction name="tryLogin" access="remote" output="false">
<cfargument name="username" type="string" required="true"/>
<cfargument name="password" type="string" required="true"/>
<cfset var result = true />
<cfreturn result />
</cffunction>
</cfcomponent>
答
当你说:
如果我在jQuery中设置我的数据类型为“html”,那么我会看到在HTML中我的整个页面的副本以及我正在查找的“true”值。
您的意思是说您看到您的“真实”值,然后是ColdFusion调试信息?如果是这样,你在你的网站中使用Application.cfm或Application.cfc吗?如果是Application.cfc,这是AJAX功能的常见问题。您需要在onRequestStart函数中捕获AJAX CFC请求,为此请求移除OnRequest函数,并禁用调试。尝试添加这对您的onRequestStart功能:
<cfif LCase(Right(ARGUMENTS.TargetPage,3)) EQ "cfc">
<cfset StructDelete(THIS,"OnRequest")>
<cfsetting showdebugoutput="no">
</cfif>
答
我想CF9改变这种行为,并且您不再需要使用onRequestStart“黑客”对阿贾克斯所谓的氟氯化碳。
我当然可能是错的 - 但我认为是这种情况...
当你说“什么都没有发生”时,你是什么意思?是否提出了要求?你可以看到它在Firebug或任何其他HTTP观看util?是否回复了回复?它是空的吗? –
在Application.cfc中,是否将this.secureJSON设置为true或false?如果为true,则将其设置为false。 –