使用cfscript函数的JSON响应 [英] JSON response using cfscript function

查看:122
本文介绍了使用cfscript函数的JSON响应的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我将此代码作为cffunction可以正常工作:

I have this code as a cffunction that works fine:

<cfcomponent extends="core.core">

<cffunction name="loadService" access="remote" returnformat="JSON">

    <cfscript>

        objResponse = '{"CONFIG":[["internal"],[ "success"]],"DATA":[["Message1"]]}';

    </cfscript>

<cfreturn objResponse>  

</cffunction>   

</cfcomponent>

我正在尝试将其转换为完整的cfscript函数,如下所示:

I am trying to convert it to a full cfscript function like this:

component extends="core.core"{

remote JSON function loadService(){

    objResponse = '{"CONFIG":[["internal"],[ "success"]],"DATA":[["Message1"]]}';

    SerializeJSON(objResponse);

    return objResponse; 
}

}

第一种方法可以很好地返回JSON,我可以使用jQuery处理它.第二个错误抛出并引发错误从loadService函数返回的值不是JSON类型."

The first way returns JSON fine and I can process it with jQuery. The second one throws and error "The value returned from the loadService function is not of type JSON."

我在有和没有Seri​​alizeJSON的情况下都尝试过,并且两种方法都抛出该错误.我也尝试过在函数语法中未指定JSON的情况.那不会抛出错误,但是它确实包裹了wddxpacket信息.这是我不指定JSON时的样子:

I have tried it with and without SerializeJSON and both ways throw that error. I have also tried it without specifying JSON in the function syntax. That does not throw an error but it does wrap wddxpacket info around it. This is what it looks like when I don't specify JSON:

<wddxPacket version='1.0'><header/><data><string>{"CONFIG":[["internal"],[ "success"]],"DATA":[["Message1"]]}</string></data></wddxPacket>

我对此感到困惑.任何帮助将是巨大的.谢谢!

I am stuck on this. Any help would be great. Thanks!

推荐答案

CF9中正确的CFScript语法为:

The correct CFScript syntax in CF9 is:

remote any function loadService() returnformat="JSON" {

从技术上讲,"JSON"不是来自函数的有效返回类型(请参阅此处以获取所有返回类型),但是当您写道:

Technically, "JSON" is not a valid returntype from a function (see here for all returntypes), but when you write:

remote JSON function

...您基本上是在说.

...you're basically saying that.

在基于标签的cffunction调用中注意,您未指定returnType ...所以您猜默认它是什么吗? (提示:任何).

Notice in your tag-based cffunction call, you do not specify a returnType...so guess what it is by default? (hint: any).

很容易将returnType和returnFormat混在一起.上面的一个简单调整,您就应该做好了.

It's easy to mix returnType and returnFormat up. A simple adjustment above and you should be good to go.

完整代码

component extends="core.core" {

remote any function loadService() returnFormat="JSON" {

    objResponse = '{"CONFIG":[["internal"],[ "success"]],"DATA":[["Message1"]]}';

    SerializeJSON(objResponse);

    return objResponse; 
}

}

这篇关于使用cfscript函数的JSON响应的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆