为什么我的 jQuery 无法识别 CFC 的返回值?
我有一个页面使用 .post 提交到我的 cfc。我知道该函数工作正常,因为数据库正在更新,但正在触发的警报是“else”语句中的警报。
你们中有人能明白为什么我的回报没有触发正确的警报吗?我没有正确捕获回报吗?
我的一些变量是为了测试目的而硬编码的......
jQuery:
$(document).ready(function() {
var theID = $('#window_one h1').attr('name').split("-")[1];
var gateway = ("001");
//Populate the form
$('#theText').attr('value', $('#window_one h1').html());
$('#texteditform').submit(function(e){
//stop the form submission
e.preventDefault()
var newText = $('#theText').val();
//CFC
$.post("cfc/engine.cfc?method=updateText&returnformat=json",
{text:newText, field:theID, gateway_id:gateway},
function(res) {
//Handle the result
if(res == "true") {
alert("worked fine");
} else {
alert("Didn't Work");
}
});
});
});
</script>
受控外国公司
<cffunction name="updateText" access="remote" output="no" returntype="boolean">
<cfargument name="field" type="string" required="yes">
<cfargument name="text" type="string" required="yes">
<cfargument name="gateway_id" type="string" required="yes">
<cfquery datasource="#application.datasource#" username="#application.username#" password="#application.password#">
UPDATE gateway
SET #arguments.field# = <cfqueryparam value="#arguments.text#" cfsqltype="cf_sql_varchar">
WHERE gateway_id = #arguments.gateway_id#
</cfquery>
<cfreturn true>
</cffunction>
I have a page that uses a .post to submit to my cfc. I know the function works fine because the database is being updated but the alert that's being fired is the one in the 'else' statement.
Can any of you see why my return isn't firing the correct alert? Am I not capturing the return properly?
Some of my variables are hard coded for testing purposes...
The jQuery:
$(document).ready(function() {
var theID = $('#window_one h1').attr('name').split("-")[1];
var gateway = ("001");
//Populate the form
$('#theText').attr('value', $('#window_one h1').html());
$('#texteditform').submit(function(e){
//stop the form submission
e.preventDefault()
var newText = $('#theText').val();
//CFC
$.post("cfc/engine.cfc?method=updateText&returnformat=json",
{text:newText, field:theID, gateway_id:gateway},
function(res) {
//Handle the result
if(res == "true") {
alert("worked fine");
} else {
alert("Didn't Work");
}
});
});
});
</script>
The CFC
<cffunction name="updateText" access="remote" output="no" returntype="boolean">
<cfargument name="field" type="string" required="yes">
<cfargument name="text" type="string" required="yes">
<cfargument name="gateway_id" type="string" required="yes">
<cfquery datasource="#application.datasource#" username="#application.username#" password="#application.password#">
UPDATE gateway
SET #arguments.field# = <cfqueryparam value="#arguments.text#" cfsqltype="cf_sql_varchar">
WHERE gateway_id = #arguments.gateway_id#
</cfquery>
<cfreturn true>
</cffunction>
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
由于 CF 生成输出的方式,您有额外的空白。您需要确保 cfc 本身设置为 output="false"...可能需要进一步调整,但这应该可以帮助您开始。
这是CF比较烦人的功能之一
You have extra whitespace because of the way that CF generates its output. You need to make sure that the cfc itself is set to output="false"... May take some further wiggling around but that should get you started.
It's one of the more annoying features of CF