gpt4 book ai didi

jquery - 我需要 Coldfusion 才能轻松处理从 AJAX 调用返回的 JSON 数据

转载 作者:行者123 更新时间:2023-12-01 04:19:21 25 4
gpt4 key购买 nike

我对远程 CFC 进行了 AJAX 调用,并按照我喜欢的方式使用 JSON 取回数据,但我在输出数据时遇到了麻烦,而不必使用硬编码索引值猜测结构索引,例如: $(' #result').val( obj.DATA[0][3] );

如果我对索引(例如 [3])进行硬编码,如果我更改 CFC 中的查询,则必须更改 AJAX 结果。所以我想通过列名称引用返回的数据,但无法弄清楚。这是我的 AJAX 和远程 CFC 的结果:

$.ajax({
url: '/app/components/MailingsReport.cfc',
//POST method is used
type: "POST",
//pass the data
data: {
method: "getCreativeByID",
creativeID: $('#hdnCreativeID').val(),
datasource: "shopping_cart",
queryformat: "column"
},
success: function(response){
var obj = $.trim(response);
var obj = jQuery.parseJSON(obj);
//alert("response");
$('#txtSubject').val( obj.COLUMNS["SUBJECT"][0] );
}
}

});

氟氯化碳:

<!---gets the data for the creative--->
<cffunction name="getCreativeByID" returntype="any" returnformat="JSON" access="remote" output="No">
<cfargument name="creativeID" required="Yes" type="numeric" />
<cfargument name="datasource" required="Yes" type="string" />

<!--- Select creatives and {clickurl} --->
<cfquery name="qGetCreativeData" datasource="#arguments.datasource#">
exec sp_get_email_creative @creativeid = #arguments.creativeID#
</cfquery>

<cfreturn qGetCreativeData />

</cffunction>

结果:

This is the result from the CFC

如有任何帮助,我们将不胜感激!谢谢。

最佳答案

这适用于除 IE8 及更低版本之外的所有浏览器。如果您需要完全兼容,您可以编写自己的indexOf JS 方法。

$('#result').val( obj.DATA[0][ obj.COLUMNS.indexOf('Creativename') ] );

如果您需要它,此页面包含有关在不直接支持它的浏览器中将indexOf添加到数组原型(prototype)的说明:http://www.tutorialspoint.com/javascript/array_indexof.htm

关于jquery - 我需要 Coldfusion 才能轻松处理从 AJAX 调用返回的 JSON 数据,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12306855/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com