【发布时间】:2020-09-01 23:46:30
【问题描述】:
我有一个通过 ajax 调用的 cfc 函数。它接受商家代码(单个代码或以逗号分隔的代码列表),执行一些检查和 I/O,然后返回一个标志。
注意: 出于安全原因,我们启用了 CFAdmin 设置“前缀 JSON 与 '//'”,这似乎是这里问题的根源。
CFC 功能:
此函数只有两种可能的返回值:字符串(例如“qweqweqwe”)或“1”。
当遇到无效的merchant_code 时,将在循环内返回字符串。
循环结束后返回“1”,表示整个过程成功。
<cffunction name="createCategoryMerchant" access="remote" returntype="any" returnFormat="JSON">
<cfargument name="category_id" required="true" type="numeric"/>
<cfargument name="merchant_code" required="true" type="any" default="" hint="expect list of merchant codes"/>
<cfset var qChk = 0 />
<cfset var qIns = 0 />
<cfset var vItem = "">
<cfloop list="#arguments.merchant_code#" index="item">
<!--- does merchant exist? --->
<cfquery name="qChk" datasource="#DSN#">
select id from merchant
where merchant_num = <cfqueryparam value="#item#" cfsqltype="cf_sql_varchar" />
</cfquery>
<cfif not qChk.recordcount>
<cfreturn merchant_code> <!--- return bad code, e.g. "qweqweqwe" --->
<cfbreak>
</cfif>
<!--- Has the merchant already been assigned to this category? --->
<cfquery name="qChk" datasource="#DSN#">
select unique_id
from category_merchant
where category_id = <cfqueryparam value="#arguments.category_id#" cfsqltype="cf_sql_integer" />
and merchant_code = <cfqueryparam value="#item#" cfsqltype="cf_sql_varchar" />
</cfquery>
<cfif qChk.recordcount>
<cfcontinue> <!--- silently accept and bail out, even though already exists --->
</cfif>
<!--- insert record if code is legit --->
<cfquery name="qIns" datasource="#DSN#">
insert into category_merchant (category_id, merchant_code)
values (
<cfqueryparam value="#arguments.category_id#" cfsqltype="cf_sql_integer" />,
<cfqueryparam value="#UCASE(item)#" cfsqltype="cf_sql_varchar" />
)
</cfquery>
</cfloop>
<cfreturn 1>
</cffunction>
AJAX:
在下面的代码中,我包含了两个我尝试过的不同的 ajax 调用选项,每个选项都会产生不同的结果,如代码 cmets 中所述。
$('.btnAddMerchant').on('click',function(e){
var clickedID = $(this).attr("id");
var category_id = clickedID.split("_")[1];
var merchant_code = $('#merchant_code').val();
merchant_code = merchant_code.replace(/\s/g,'');
$('.res').empty().hide;
if(merchant_code.length == 0){
$('#resAdd').show().html('Enter merchant code');
}else{
// OPTION 1: Note I have used "dataFilter" to handle the "//" prefixed JSON
$.ajax({
type:"POST",
url: '/system.cfc?method=createCategoryMerchant',
data: {category_id:category_id,merchant_code:merchant_code},
dataFilter: function(data, type) {return data.substr(2)},
dataType: 'json',
cache:false,
success: function(res) {
alert(res); // This only fires for INVALID codes, i.e. if the cfc returns a string such as "qweqweqwe". Doesn't fire for a single numeric return, e.g. "1"
}
});
// OPTION 2:
$.getJSON("/system.cfc?method=createCategoryMerchant&returnformat=json",{"category_id":category_id,"merchant_code":merchant_code},function(res,code){
alert(res); // This only fires if the code is VALID, i.e. the CFC returns "1". It does not fire if the invalid code "qweqweqwe" is returned
});
}
});
还请注意:仅返回带有“//”前缀的无效代码。如果返回“1”,则没有前缀。
如果我禁用 CFAdmin 设置“使用 '//' 作为 JSON 前缀”,那么所有问题都会消失。 我更喜欢使用上面的 ajax 选项 1,但需要知道为什么它显然只是默默地无法处理返回的标志“1”以获取有效数据。
编辑
问题可能只是选项 1 中的 dataFilter 属性使用的返回值没有以“//”为前缀。那为什么返回的“1”没有带“//”前缀,而“qweqweqw”却有前缀呢?
【问题讨论】:
-
Tbh,我很惊讶它完全有效,因为它们都不是真正有效的 json。我猜 CF 只是使用一个简单的算法,例如,结果是一个字符串,返回格式是 json。它可能将 1 视为数字并跳过它。无论如何,如果函数返回一个有效的 json 结构/字符串而不是一个简单的值,它可能会始终如一地工作。
标签: json ajax coldfusion coldfusion-2018