简体   繁体   English

如何从CFFUNCTION传递JSON

[英]How to pass JSON from CFFUNCTION

I am new to JSON, in fact Javascript. 我是JSON的新手,实际上是Javascript。 I actually found the code from someone and try to implement this for what I am trying to do. 实际上,我从某人那里找到了代码,并尝试实现此功能。

On my javascript I have: 在我的JavaScript上,我有:

function signin(var1,var2) {
Signin('login.htm?var1=' + var1 + '&var2=' + var2);
}

function skypeme(var1, var2) {
    $.ajax({
        url: "skypeme.cfc",
        dataType: "json",
        data: {a:var1, b:var2},
        success: function(userData) {
            if(userData.LOGIN == 'true' && userData.ID!='') {
                window.location = 'skype:'+userData.ID+'?chat'; 
            }
            else {
                signin(var1,var2);
            }
        }
    });
}

And on my skypeme.cfc: 在我的skypeme.cfc上:

<cfcomponent output="false"> 
<cffunction name="cfskypeme" access="remote" returnformat="JSON" output="false"> 
<cfargument name="a" required="true"> 
<cfargument name="b" required="true">  
<cfset var stReturn = structNew()>
<cfset stReturn.LOGIN=false>
<cfset stReturn.ID="">

if user login
    check database for skypeid
        <cfif skypelist.recordcount eq 1>
           <cfset stReturn.LOGIN=true>
           <cfset stReturn.ID=skypelist.skype>
        </cfif>
        <cfreturn SerializeJSON(stReturn)>
if user not login
    <cfreturn SerializeJSON(stReturn)>

</cffunction> 

</cfcomponent>

What I want to do is call the cfc and check if user is login, if not call the signin function. 我想做的是调用cfc并检查用户是否登录,如果没有调用登录功能。 If the user is already signed in, check the database. 如果用户已经登录,请检查数据库。 If skype id is found, kick up skype, if not do nothing. 如果找到了Skype ID,请踢出Skype,如果不执行任何操作。 But what is happening is when I called the skypme() function from my html, nothing happen. 但是发生的事情是当我从html调用skypme()函数时,什么也没有发生。 No error but skype doesn't start. 没有错误,但Skype无法启动。

What is the right way of passing the JSON value from cffunction so that it can be executed on the javasctipt side? 从cffunction传递JSON值以便可以在javasctipt端执行的正确方法是什么? I have tried many ways but just can't get it to work. 我尝试了很多方法,但无法正常工作。 Please help. 请帮忙。 Thank you. 谢谢。

A couple of issues: your the success portion of your ajax call will always execute if skypeme.cfc returns 200 OK (html header), so returning "false" will actually return the string "false", and success will execute. 有两个问题:如果skypeme.cfc返回200 OK(html标头),则始终执行ajax调用的成功部分,因此返回“ false”实际上将返回字符串“ false”,并且成功将执行。 What you should do is always return a JSON string, and have the format be always the same (also why are you using a query object? use a structure): 您应该做的是始终返回一个JSON字符串,并且格式必须始终相同(这也是为什么要使用查询对象?使用结构):

<cfcomponent output="false"> 
    <cffunction name="cfskypeme" access="remote" returnformat="JSON" output="false"> 
        <cfargument name="var1" required="true"> 
        <cfargument name="var2" required="true">  
        <cfset var stReturn = structNew()>
        <cfset stReturn.LOGIN=false>
        <cfset stReturn.ID="">

            <cfif user login logic>
                <cfquery name="skypelist"> <!---check db for skypeid--->
                </cfquery>
                <cfif skypelist.recordcount eq 1>
                    <cfset stReturn.LOGIN=true>
                    <cfset stReturn.ID=skypelist.skype>
                </cfif>
            </cfif> <!--- end login logic --->

        <cfreturn SerializeJSON(stReturn)>
    </cffunction> 
</cfcomponent>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM