Jquery Coldfusion:将json从一个CFM传递到另一个CFM?
基本上我是从一个CFM调用另一个CFM,它创建一个对象,调用这个对象上的几个方法,并让用户登录;否则,它会在屏幕上打印错误 除了打印错误,还有没有办法获取CFM并让它向调用它的文件发送JSON对象 以下是我的ajax:Jquery Coldfusion:将json从一个CFM传递到另一个CFM?,jquery,coldfusion,coldfusion-8,Jquery,Coldfusion,Coldfusion 8,基本上我是从一个CFM调用另一个CFM,它创建一个对象,调用这个对象上的几个方法,并让用户登录;否则,它会在屏幕上打印错误 除了打印错误,还有没有办法获取CFM并让它向调用它的文件发送JSON对象 以下是我的ajax: // processing the login form using jQuery $("#loginForm").submit(function(event){ // prevents the form from being submitted, the
// processing the login form using jQuery
$("#loginForm").submit(function(event){
// prevents the form from being submitted, the event is the arg to the function
event.preventDefault();
// stores the data from the form into a variable to be used later
dataString = $("#loginForm").serialize();
// the AJAX request
$.ajax
({
type: "POST",
url: "/helpers/auth/ldap/login_demo.cfm",
data: dataString,
//dataType: "text",
success: function()
{
location.reload();
},
error: function(ErrorMsg)
{
$("#hiddenLoginError").show("fast");
$("#loginForm p").css("margin-bottom","3px");
}
});
});
尝试:
#序列化JSON(对象)#
在您的CFM页面上,您可以有一个
语句来检查页面是否是用ajax请求的。如果是ajax,则返回一个json对象
<cfset isAjax = (isDefined('cgi.http_x_requested_with') AND lcase(cgi.http_x_requested_with) EQ 'xmlhttprequest')>
...
<cfif isAjax>
<cfcontent reset="true">{ "result":false, "message": "Login Failure" }<cfabort>
</cfif>
因为它返回数据,所以success函数总是真的吗?因为它返回一个200头,除非您另外指定,它总是运行success函数。success函数中的数据基于您返回的内容。在我的示例中,我返回了一个带有两个键的对象。第一个键,
result
,包含一个等于false的布尔值。我的代码显示了一个登录失败的示例响应。有没有方法可以使用成功区域设置javascript变量?我尝试了myVar=rdata.message;没有运气。
<cfset isAjax = (isDefined('cgi.http_x_requested_with') AND lcase(cgi.http_x_requested_with) EQ 'xmlhttprequest')>
...
<cfif isAjax>
<cfcontent reset="true">{ "result":false, "message": "Login Failure" }<cfabort>
</cfif>
dataType: "json",
success: function(rdata) {
alert(rdata.message);
}