2015-09-02 35 views
0

我编写了这段代码。基本上它每2秒检查一次数据库以查看是否有更改。如果没有发生变化,则一个变量(CheckMessageCount)返回值为0并且什么都不会发生。如果进行更改并且CheckMessageCount传回的值为1,则div会在屏幕上重新加载消息。转换要在Ajax语句中读取的coldfusion变量

我不确定是如何将cfm页面中的CheckMessageCount传递回jquery/ajax,以便if/else语句可以正常完成。现在它只是一直到别的地方,什么都不做。

我读了如何做一个查询字符串,但我不能让它工作。我也尝试做this thread做了什么,但没有运气。

下面是我用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.cfc

<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,但是我们的托管公司“不支持他们在服务器上”,而且它真的超出了我的覆盖范围。我很惊讶,我对此做了这么多。任何帮助,将不胜感激。

只有原因我知道它半工作,因为我把reload语句放在JS的else部分,它重新加载div。

+0

我也建议使用这种方法的cfc,你不是在任何地方输出数值 - 你只是简单地设置它。这就是为什么没有东西被发回jQuery。 –

+0

另外,CFIF中'messagecount'变量的来源和用途是什么?如果它应该是传递给脚本的某种参数,它必须包含在ajax调用中。 – Leigh

+0

丹,我更新了代码以包含cfc并更新了JS,因为我认为我有一些不需要的东西。 Leigh,消息计数在cfc中设置,我没有包含代码,因为它工作并使if语句有效。 任何想法为什么javascript无法识别cfc传回0或1? – Adam

回答

0

这将会是最好的,如果你使用CFC,但在你的例子做的权利之前

<cfcontent type="application/json">#checkMessageCount#</cfoutput> 

这将返回一个值与JSON MIME类型。你可以做serializeJSON,但没有什么可以序列化的。由于重置属性默认为0

你也需要改变“CheckMessageCount”到“returnresult”在您的JS

cfcontent将删除所有的间距。

请记住,您的解决方案不具备真正的可扩展性,并会导致您的系统受到任何体面的流量困扰。

确保此CFM页面的调试已关闭,否则会中断JSON。

+0

它会改变cfc中的任何内容吗?我有一个基本完成相同的事情,并返回 Adam

+0

使用CFC给你更多的代码结构。你不会输出var,而是像你提到的那样返回它。更多的是喜好。 –

+0

Chris,我更新了cf和js代码。我意识到我可能在那里有一些错误的ajax东西,你能再看看它吗?如果返回值为1,我仍然无法获得javascript注册。 – Adam