我想了解有关coldfusion的ajax调用,以便我可以在不重新加载主页的情况下显示数据。举个简单的例子。点击按钮添加两个数字。为此,我需要两个输入元素和一个按钮,它应该显示在同一页面中。我想要做的是,当我从用户那里得到输入时,他们点击我需要回答,而无需重新加载页面。所以我用组件文件和书面函数为我的场景......我通过ajax调用我的组件。但是我的东西出错了。让我表明我做了什么。如何从ajax调用coldfusion组件文件
ColdFusion代码
<!doctype html>
<html>
<head>
<meta charset="UTF-8">
<title>Adding two numbers</title>
<script type="text/javascript" src="http://code.jquery.com/jquery-latest.min.js"></script>
</head>
<body>
<form>
<!--two numbers to add when clicking a button it will show my result in display-->
FIRST NUMBER<input type="text" id="a" name="a" >
SECOND NUMBER<input type="text" id="b" name="b">
<input type="button" name="button" id="button" value="add">
ANS<input type="text" name="display" id="display" readonly>
</form>
</body>
<script language="JavaScript">
$(document).ready(function() {
$("#button").click(function(f) {
// Next, we will be needing the value of the textfield in order to pass it to ColdFusion.
var variableToPass = $("input#a").val();
var variableToPass1 = $("input#b").val();
$.ajax({
url: "add.cfc?method=add&returnformat=json",
data: {
a: variableToPass,
b: variableToPass1
},
success: function(result) {
$("#display").val(result);
}
});
});
});
</script>
</html>
我保存这个作为add.cfm,这里是我的组件文件即(add.cfc)
<cfcomponent>
<cffunction name="add" access="remote" returntype="numeric">
<cfargument name="a">
<cfargument name="b">
<cfset c=a+b>
<cfreturn c>
</cffunction>
</cfcomponent>
在运行我的代码...我抛出错误输入错误和更多警告...请帮助我
变化的提交按钮ID点击现在是“按钮” –
现在不太一样issueeeee .... –
为什么你returnFormat = json的,当你只是返回数值数据? –