2016-02-27 65 views
0

我想了解有关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> 

在运行我的代码...我抛出错误输入错误和更多警告...请帮助我

+0

变化的提交按钮ID点击现在是“按钮” –

+0

现在不太一样issueeeee .... –

+0

为什么你returnFormat = json的,当你只是返回数值数据? –

回答

0

将returntype number更改为numeric。因为没有返回类型number for coldfusion功能。在你的榜样,而不是$("#click")

<cffunction name="add" access="remote" returntype="numeric"> 

注意你应该使用$("#button")

然后使用val()而不是html(),因为这是如何将值添加到input字段。

$("#display").val(result); 
+0

现在太同样的问题 –

+0

@SowmyaSow究竟是什么错误? – RRK

+0

我没有看到任何在Coldfusion服务器中的任何错误...我没有得到任何输出在我的看法太... bt当我看到firefor的firefor,我可以看到typedef错误和很多警告 –