微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

带前缀“ //”的JSON返回会产生不一致的结果

如何解决带前缀“ //”的JSON返回会产生不一致的结果

我有一个通过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调用选项,每个选项都会产生不同的结果,如代码注释中所述。

$('.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”,则没有前缀。

enter image description here

如果禁用CFAdmin设置“使用'//'前缀JSON”,则所有问题都会消失。 我希望使用上面的ajax选项1,但是需要知道为什么它显然只是地无法处理有效数据的返回标志“ 1”。

编辑

问题可能仅仅是因为选项1中的dataFilter属性正在使用未以“ //”作为前缀的返回值。那么,为什么没有以“ //”作为前缀返回“ 1”,而以“ qweqweqw”作为前缀的原因呢?

解决方法

我已使用以下方法解决了此问题:

<cfreturn serializeJSON(1)>

这似乎是将“ //”前缀手动添加到“ 1”。不确定为什么一开始就没有它。

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。