在AJAX javascript中从ASHX获取数据
Getting data from ASHX in AJAX 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
如有任何帮助/建议,我们将不胜感激。非常感谢。
调用处理程序时缺少函数名。请更改代码如下:
url: "handlecb.ashx/ProcessRequest",
type: "POST",
相关文章:
- Ajax-如何获取数据
- 从键值结构中获取数据,并将其与AngularJS中ng重复的值进行比较
- 从数据库中获取数据并插入JavaScript变量
- 从单击的行上的列中获取数据
- Solr查询以按日期月份获取数据&年
- React路由器服务器端渲染和ajax获取数据
- 使用JavaScript在IE9中获取数据列表选项
- 从json子数组获取数据
- 使用angularjs中的rest调用通过id获取数据
- 正在从ruby应用程序中的数据库中获取数据
- 如何从servlet获取数据到ajax成功
- 从选择下拉菜单中获取数据
- 异步获取数据使用JavaScript同步获取数据
- 从服务器获取数据并在选择控件中使用ng选项无法显示选项
- 如何在Angular.js中循环动态添加Fields并获取数据并将其发送到服务器
- JavaScript-获取数据属性的值返回未定义的值
- 如何从文本区域获取数据并使用javascript进行解密
- 在put方法之前从作用域获取数据
- 事件循环的MEAN.JS setInterval进程(从另一个服务器获取数据)
- 使用ReactJS和Flux架构从服务器获取数据的正确方法是什么