我建立了SessionMgr.cfc
<cffunction name="jgetValue" access="remote" returntype="string" output="yes" returnFormat="json">
<cfargument name="variablename" type="string" required="yes">
<cfset var result = 0>
<cfset result = Evaluate("session" & "." & arguments.variablename)>
<cfset var ReturnValue = result />
<cfreturn result />
</cffunction>
在Coldfusion中设置/获取会话变量以运行所有$ .ajax调用,而且我似乎做错了什么。我已阅读并阅读了stackoverflow,谷歌可以在该主题上提供的每页内容,也许有人可以解释我做错了什么。
这里是我的getter();、我的getValue()和getCalBack();,正确的值在getValue()中是正确的,但是我尝试过的所有操作都将[object Object]返回给getCAllBack()处理函数;这是我的代码;
// rID in this instance is session.rID
var tReportID = getValue('rID');
alert(tReportID);
function getValue(a) {
return $.ajax({
url: "cfc/SessionMgr.cfc",
type: "get",
dataType: "text",
data: {
method: "jgetValue",
variablename: a
},
success: function(response) {
obj = JSON.parse(response);
//alert('in getValue: ' + obj);
console.log('getValue: ' , a , ' value: ' , JSON.parse(response));
},
error: function(msg) {
console.log(msg);
}
});
//alert(' in returnVal: ' + obj);
}
任何帮助将不胜感激。因此,我将代码更新为您的建议Prince,但是我仍然在警报中得到[object Object]。
如果我分解对象,如何获取responseText?它具有正确的价值; responseText:“ 12”
改用cfajaxproxy
<cfajaxproxy cfc="CFC/SessionMgr" jsclassname="SessionMgr" />
然后进入
尝试更改:
dataType: "text",
AJAX调用返回的值是一个对象,其功能正常。问题是,当您使用串联运算符将其安慰时,它将其转换为字符串。
不要使用串联,请按照以下代码进行操作:
var mydata;
function getValue(a) {
return $.ajax({
url: "cfc/SessionMgr.cfc",
type: "get",
dataType: "text",
data: {
method: "jgetValue",
variablename: a
},
success: function(response) {
mydata = JSON.parse(response);
console.log('getValue: ', a , ' value: ' , JSON.parse(response));
},
error: function(msg) {
console.log(msg);
}
});
}
getValue(a);
console.log(mydata)
我不知道您为什么要使其复杂,您可以通过在成功块内为它赋一个变量并为其分配响应值来简化它。