2016-04-03 52 views
0

創建: 處理程序在數據庫中檢查是否有新的消息。在AJAX中從ASHX獲取數據javascript

我到目前爲止所做的: 使用AJAX的Javascript每三秒運行一次處理程序。

我無能爲力:將結果從ASHX處理程序返回給AJAX javascript,以便在結果爲true時執行回發。

這裏是JavaScript的:

//handle message upload 

     window.setInterval(function() { 

      $.ajax({ 
       url: "handlecb.ashx", 
       type: "POST", 

       success: function (result) { 
        toastr.options = { 
         "closeButton": false, 
         "debug": false, 
         "newestOnTop": true, 
         "progressBar": false, 
         "positionClass": "toast-bottom-left", 
         "preventDuplicates": true, 
         "onclick": null, 
         "showDuration": "1300", 
         "hideDuration": "1300", 
         "timeOut": "5300", 
         "extendedTimeOut": "1000", 
         "showEasing": "swing", 
         "hideEasing": "linear", 
         "showMethod": "fadeIn", 
         "hideMethod": "fadeOut" 
        } 
        toastr.info('Online.'); 

        if (result < 0) { 
         __doPostBack("<%= senbut.ClientID %>", ""); 
         toastr.options = { 
          "closeButton": false, 
          "debug": false, 
          "newestOnTop": true, 
          "progressBar": false, 
          "positionClass": "toast-bottom-left", 
          "preventDuplicates": true, 
          "onclick": null, 
          "showDuration": "1300", 
          "hideDuration": "1300", 
          "timeOut": "5300", 
          "extendedTimeOut": "1000", 
          "showEasing": "swing", 
          "hideEasing": "linear", 
          "showMethod": "fadeIn", 
          "hideMethod": "fadeOut" 
         } 
         toastr.success('New message recieved.'); 
        } 


       }, 
       error: function (err) { 
        toastr.options = { 
         "closeButton": false, 
         "debug": false, 
         "newestOnTop": true, 
         "progressBar": false, 
         "positionClass": "toast-bottom-left", 
         "preventDuplicates": true, 
         "onclick": null, 
         "showDuration": "1300", 
         "hideDuration": "1300", 
         "timeOut": "5300", 
         "extendedTimeOut": "1000", 
         "showEasing": "swing", 
         "hideEasing": "linear", 
         "showMethod": "fadeIn", 
         "hideMethod": "fadeOut" 
        } 
        toastr.danger('Offline'); 

       } 
      }); 
     }, 3000); 

這裏的處理程序:

<%@ WebHandler Language="VB" Class="handlecb" %> 

    Imports System.Data 
    Imports System.IO 
    Imports System.Data.OleDb 
    Imports System.Web.SessionState 
    Imports System.Web.Security 
    Imports System.Web 

     Public Class handlecb 
      Implements IHttpHandler, System.Web.Session 

State.IRequiresSessionState 

    Dim con As OleDbConnection = New OleDbConnection("Provider=Microsoft.ACE.OLEDB.12.0;Data Source=C:\Users\Ishaan Patel\Desktop\Paperhome\paperhome_data.accdb") 


Dim cmd As OleDbCommand 
Dim cmd2 As OleDbCommand 
Dim da As OleDbDataAdapter 
Dim ds As New DataSet 
Dim dr As OleDbDataReader 
Dim result As Integer 

Public Sub ProcessRequest(ByVal context As HttpContext) Implements System.Web.IHttpHandler.ProcessRequest 
    Dim threadid As String 
    If Not context.Session("chatsess") Is Nothing Then 
     threadid = context.Session("chatsess") 
     GoTo b 
    Else 
     GoTo a 
    End If 
    b: 
    Dim thread As Integer 
    thread = Convert.ToInt32(threadid) 


    con.Open() 'checklastupdatedon 
    cmd = New OleDbCommand("SELECT [last_updated_on] FROM [message_threads] WHERE ([message_threads].[thread_id] = " & thread & ")", con) 
    cmd.Connection = con 
    dr = cmd.ExecuteReader 
    If (dr.Read) Then 
     Dim fetched As Date = Convert.ToDateTime(dr("last_updated_on")) 
     Dim old As Date = Convert.ToDateTime(context.Session("last_updated_on")) 

     result = DateTime.Compare(old, fetched) 
     con.Close() 

     If result < 0 Then 'old fetched is earlier than timer fetched 

      context.Session("last_updated_on") = fetched 

     End If 

    End If 
    a: 
End Sub 

Public ReadOnly Property IsReusable() As Boolean Implements IHttpHandler.IsReusable 
    Get 




    Return result 
    End Get 
End Property 
End Class 

任何幫助/建議表示讚賞。謝謝。

回答

0

調用處理程序時,函數名缺失。 請更改代碼如下:

url: "handlecb.ashx/ProcessRequest", 
type: "POST", 
+0

是否做到了這一點。它仍然不會通知收到新消息的時間。我試圖按下按鈕的邏輯,它工作正常,所以我相信處理程序不是問題。我返回的結果是正確的嗎? –

+0

請點擊按鈕後發佈您收到的回覆。 – user3151766

+0

沒有按鈕點擊發生或應該發生。所有這一切都應該沒有任何按鈕點擊工作。雖然清除 - 結果<0中的代碼被執行。引導我的方式使我可以修復此塊內的值,並將相同的值從處理程序發送到JavaScript。 –