转换要在Ajax语句中读取的coldfusion变量

Converting a coldfusion variable to be read in Ajax statement

本文关键字:读取 coldfusion 变量 语句 Ajax 转换      更新时间:2023-09-26

我写了这段代码。基本上,它每2秒检查一次数据库,看看是否有更改。如果没有进行任何更改,则变量(CheckMessageCount)返回值为0,并且没有发生任何变化。如果进行了更改,并且CheckMessageCount返回值为1,则div将重新加载屏幕上的消息。

我不确定的是我如何通过CheckMessageCount从cfm页面回到jquery/ajax,所以if/else语句可以正确完成。现在它只是一直转到else,什么也不做。

我读了如何做一个查询字符串,但我不能使它工作。我也试着做这个线程所做的,但没有运气。

这是我用Jquery/AJAX的页面

<html>
<head>
    <title>Test</title>
    <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.3/jquery.min.js"></script>
    <script>
    $.get("homepageresponse2.cfm", function (data) //gets the code behind the page
        { 
            $("#responsecontainer").html(data); //places the data in the budget div
        });
    var CheckMessageCount = 0;
    (function() {
        var reloadthediv = function() {
        $.ajax({
            url: "CheckMessageCount.cfc?method=ReloadMessageCount",
            type: "get",
            datatype: "json"
        })
            .done(function(CheckMessageCount) {
            //if checkmessagecount comes back 1, then reload
            if (CheckMessageCount == 1) {
                $( "#responsecontainer" ).load( "homepageresponse2.cfm" );
            //if checkmessagecount comes back 0, do nothing
            } else {
                //do nothing
                console.log('nothing done')
            } 
        });
    };
    reloadthediv();
    setInterval(function() {
        reloadthediv();
    }, 2000);
    })();
</script>
</head>
<body>
    <div id="responsecontainer" class="responsecontainercss"></div>
</body>
</html>

这是我为CheckMessageCount做的。氯氟化碳

    <cfcomponent displayname="Checks The Message RecordCount to reload" hint="This is the CFC for Checks The Message RecordCount to reload"> 
    <!--- This function gets all user status data for the proper session.id if it exists ---> 
        <cffunction name="ReloadMessageCount" returntype="any" returnformat="json" hint="Gets message count"> 
        <cfquery name="CheckMessages" datasource="hippogriff" debug="No">
        SELECT  COUNT(HomePageMessage_ID) AS TheMessageCount
        FROM    HomePageMessage
        </cfquery>
        <!--- code here for the messagecount --->
        <!--- once it does, let's do this --->
        <cfif CheckMessages.TheMessageCount GT messagecount>
            <cfset CheckMessageCount=1> 
            <cfset messagecount=#CheckMessages.TheMessageCount#>
        <cfelse>
            <cfset CheckMessageCount=0> 
        </cfif> 
        <cfreturn CheckMessageCount> 
    </cffunction> 
</cfcomponent>

这是我所知道的。我尝试了ColdFusion serializeJSON,但没有得到任何结果。我知道现在有websockets,但我们的托管公司"在服务器上不支持它们",而且我真的无法编码。我很惊讶我能走到这一步。如有任何帮助,不胜感激。

我知道它能正常工作的唯一原因是因为我把重载语句放在了JS的else部分,它重新加载了div。

最好使用CFC,但在您的示例中,在

之前执行此操作
<cfcontent type="application/json">#checkMessageCount#</cfoutput>

这将返回一个JSON MIME类型的值。您可以执行serializeJSON,但实际上没有什么要序列化的。

cfcontent将删除所有的空格,因为reset属性默认为0。

你还需要改变"CheckMessageCount"为"returnresult"在你的js。

请记住,您的解决方案不是真正可扩展的,并且最终会使您的系统陷入任何像样的流量。

请确保关闭这个返回JSON的CFM页面的调试,否则它将中断。