You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

ASP.NET/Visual Basic代码报500错误,DataSource.read()无法获取VB后端数据

问题排查与修复方案

核心问题汇总

  • 服务端配置与路径不匹配:前端请求的路径是/webservices/alertService.asmx/GetAlert,但你的WebService类名为MapService,类名必须和asmx文件名保持对应。同时默认ASMX的WebMethod仅支持POST请求,没有额外配置的话GET请求会直接被拦截返回500错误。
  • 手动拼接JSON语法错误:你拼接的JSON每个对象最后一个属性后多了一个多余的逗号,属于无效JSON格式,而且如果alert内容包含双引号、换行等特殊字符,会直接破坏JSON结构。
  • 异常被完全吞掉:所有Catch ex As Exception块都没有任何日志输出,数据库连接失败、字段不存在、查询报错等问题都会被直接忽略,无法定位500错误的具体原因。
  • 前端异步方法调用错误:dataSource.read()是异步方法,返回的是Promise对象,不是接口返回的实际数据,直接读取alertObj.alert_title必然拿不到内容。
  • 数据库资源未正确释放:没有使用Using块管理ODBC连接、命令、阅读器对象,容易出现数据库连接泄漏,引发后续请求失败。

修复后的代码

后端VB代码

<%@ WebService Language="VB" Class="AlertService" %>
' 类名必须和asmx文件名保持一致,如果你的asmx文件叫alertService.asmx,类名就用AlertService
Imports System
Imports System.Configuration
Imports System.Data
Imports System.Data.Odbc
Imports System.Web.Services
Imports System.Web.Script.Services

<ScriptService()> _
<WebService(Namespace:="http://tempuri.org/")> _
<WebServiceBinding(ConformsTo:=WsiProfiles.BasicProfile1_1)> _
<ToolboxItem(False)> _
Public Class AlertService
    Inherits System.Web.Services.WebService

    <WebMethod()> _
    <ScriptMethod(UseHttpGet:=True, ResponseFormat:=ResponseFormat.Json)> _
    Public Function GetAlert() As Object
        Dim alertData As New Dictionary(Of String, Object)()
        Try
            Dim sConnString As String = ConfigurationManager.ConnectionStrings("WebApp").ConnectionString
            ' 使用Using块自动释放连接资源,无需手动关闭
            Using odbcConn As New OdbcConnection(sConnString)
                Dim sQueryString As String = "SELECT alert_id, alert_title, alert_body, show_alert FROM tblalert WHERE alert_id = 1"
                Using DBCommand As New OdbcCommand(sQueryString, odbcConn)
                    odbcConn.Open()
                    Using odbcReader As OdbcDataReader = DBCommand.ExecuteReader(CommandBehavior.CloseConnection)
                        If odbcReader.Read() Then
                            alertData("alert_id") = Convert.ToInt16(odbcReader("alert_id"))
                            alertData("alert_title") = Trim(odbcReader("alert_title").ToString())
                            alertData("alert_body") = Trim(odbcReader("alert_body").ToString())
                            alertData("show_alert") = Convert.ToInt16(odbcReader("show_alert"))
                        End If
                    End Using
                End Using
            End Using
        Catch ex As Exception
            ' 可在此处添加日志记录异常信息,方便排查错误
            alertData("error") = ex.Message
        End Try
        ' 直接返回对象,框架会自动序列化为合法JSON,无需手动拼接和Response.Write
        Return alertData
    End Function
End Class

前端代码

<asp:Content ID="BodyContent" ContentPlaceHolderID="MainContent" runat="server">
    <script>
        var dataSource = new kendo.data.DataSource({ 
            transport: {
                read:  {
                    url: "/webservices/alertService.asmx/GetAlert",
                    dataType: "json"
                },
                update: {
                    url: "/webservices/alertService.asmx/UpdateAlert",
                    dataType: "json"
                },
                // ASMX返回的JSON默认包裹在d字段中,需要提取实际内容
                parameterMap: function(data, type) {
                    return kendo.stringify(data);
                },
                schema: {
                    data: function(response) {
                        return response.d;
                    }
                }
            }
        });
        // read是异步方法,需在请求完成的回调中处理返回数据
        dataSource.bind("requestEnd", function(e) {
            var alertObj = e.response.d;
            console.log("alertObj: ", alertObj);
            var pageBody = document.getElementById('page-body');
            
            pageBody.innerHTML = `<h1>Website alert page is currently being built...</h1><br/>
                                 <p>${alertObj.alert_title || '无标题'}</p><br/>
                                 <p>${alertObj.alert_body || '无内容'}</p><br/>`
        });
        dataSource.read();
    </script> 

    <div id="page-body"></div>
</asp:content>

额外配置说明

如果修改后仍然提示请求被拒绝,请在web.config的<system.web>节点中添加以下配置,允许GET请求调用WebMethod:

<webServices>
  <protocols>
    <add name="HttpGet"/>
    <add name="HttpPost"/>
  </protocols>
</webServices>

内容的提问来源于stack exchange,提问作者user13989216

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.10.19 06:45:04