2016-02-29 87 views
1

我有一个表单设置,我从列表中选择一个名称,调用CFC并针对该名称运行查询。然后我从该查询中返回一美元金额。使用CFC JSON结果填充输入字段

我然后试图拿出这个美元金额并且改变一个<input>元素的值。

我可以成功运行我的CFC并返回正确的值,但我的JQUERY不会更改我的输入字段的值。它跳过我的成功:方法和正确的我的错误:方法。

这里是我的表单代码:

<cfselect class="required" queryPosition="below" query="get_ticket" display="company_name" name="customer_checkout" id="customer_checkout" tabindex="0" onchange="PopulateGrandTotal();" ><option>---Make A Selection---</option></cfselect> 

<div id="grant_totalDIV" > 
     <input type="number" name="grand_total_due" id="grand_total_due"> 
</div> 

这里是我的CFC:

<cffunction name="getTotal" access="remote" returntype="any"> 
<cfargument name="customer_checkout" type="any" required="true"> 

<!--- localize function variables ---> 
<cfset var dataDetail = ""> 

<cfquery name="dataDetail" datasource="#datasource#" > 
select grand_total 
from service_ticket 
where company_name = '#customer_checkout#' 
</cfquery> 


<cfoutput query="dataDetail"> 

    <cfreturn dataDetail.grand_total> 
</cfoutput> 
</cffunction></cfcomponent> 

这里的JQuery:

<script> 
function PopulateGrandTotal(){ 
    // Populate the customer alert DIV based on the customer selection 
    console.log($("#customer_checkout>option:selected").attr("Value")); 

    $.ajax({ 
     url:'cfcs/grand_totalDIV.cfc?method=getTotal&returnformat=json', 
     dataType: 'json', 
     data: { customer_checkout: $("#customer_checkout>option:selected").attr("Value") }, 

     success: function(response) { 
      console.log('Successfully ran JSON, now changing input value'); 
      $("#grand_total_due").val(response); 

      console.log('Input value cahnged'); 

      }, 
     error: function(response){ 

       console.log('Error'); 

       } 
     }); 
} 
</script> 

我的JSON响应:

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> 
<head> 
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" /> 

</head> 
<body> 

</body> 
274.00 

任何人都可以帮忙吗?

回答

2

它看起来像你只是简单地返回文本,所以不要使用dataType: 'json',因为这不是发送的内容。

您可以直接从阿贾克斯配置

否则删除选项,您将需要JSON序列化结构和您的成功处理程序中访问正确的属性

+0

好了,我注释掉的代码的一部分,现在它运行成功:方法,因为我可以看到它记录我在那里编码的消息。但它不会改变我的输入字段的值与响应。我的代码是否正确? –

+0

尝试登录响应安慰 – charlietfl

+0

更新:我改变了输入字段的类型从数量到文本,现在它改变了我的价值,这一点: 274.00 –

1

(太长的评论)

您还应该对齐返回类型,因此双方都同意返回什么类型的数据以及以何种格式。如果你喜欢返回纯文本,而不是JSON:

  • 设置CFFunction使用适当的返回类型,即returntype="numeric"returntype="string"
  • 然后告诉CF的格式应该通过提供returnformat参数使用,并使用dataType来告诉jQuery它应该期望收到什么类型的数据。正如评论中提到的那样,默认是做一个intelligent guess: (xml, json, script, or html)。但是,提供它并不会伤害任何东西,并且明确指定它有助于避免像最初的json/text问题IMO那样的无意混淆。

    $.ajax({ 
        url:'cfcs/grand_totalDIV.cfc?method=getTotal&returnformat=plain', 
        dataType: 'text', 
        ... 
    }); 
    

此外,无关你的错误,但有几个技巧:

  • 由于该功能实际上并没有显示任何内容,也没有必要为<cfoutput query="...">。只需返回单个值:<cfreturn dataDetail.grand_total>
  • 如果函数需要customer_checkout(即字符串,数字等等)的特定类型的值,请在参数签名中指定它,而不是使用type="any"
  • 始终使用cfqueryparam以提高性能并防止SQL注入
  • 不要忘记范围内的所有变量,IE的arguments.customer_checkout代替customer_checkout
+0

FYI'纯朴'是不是一个有效的数据类型...“文本”..或“H​​TML”将是但智能猜测内置将默认为文本/ HTML无论如何 – charlietfl

+0

@ charlietfl - 哎呀,谢谢。这是一个复制+粘贴错误。我会更新它。当然,它可以猜测,但我建议调整类型主要是为了强调“格式”适合图片的位置,并且双方之间存在关系(老实说,它似乎并不完全清楚给提问者)。 – Leigh

相关问题