我的 ASP.NET/Visual Basic 代码有什么问题?

问题描述

我是 ASP.NET / Visual basic 的新手。我正在尝试创建一个简单的 get 请求来从数据库获取数据。但是每当页面加载时,我都会在控制台中收到一个通用的 500 服务器错误代码。这是我的 ASP.NET 代码

<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"
                                        }
                         }});
        var alertObj = dataSource.read();
        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/>"  
    </script> 

    <div id="page-body"></div>

</asp:content>

这是我的 Visual Basic 代码

<%@ WebService Language="VB" Class="MapService" %>

Imports System
Imports System.IO
Imports System.Security
Imports System.Configuration
Imports System.Xml
Imports System.Web
Imports System.Web.Script.Serialization
Imports System.Web.Script.Services
Imports System.Web.Services
Imports System.Web.Services.Protocols
Imports System.ComponentModel
Imports System.Diagnostics
Imports System.Xml.Serialization
Imports MysqL.Data.MysqLClient
Imports System.Data
Imports System.Data.Odbc
Imports System.Data.sqlClient
Imports System.Data.sql
Imports System.Collections.Generic
Imports Newtonsoft.Json
Imports System.Net.Http

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

<WebMethod()> _
Public Sub GetAlert()
    Dim xmlString As String = ""

    Try
        Dim sConnString As String = ConfigurationManager.ConnectionStrings("WebApp").ConnectionString
        Dim odbcConn As OdbcConnection = New OdbcConnection(sConnString)
    
        Dim sQueryString As String = "SELECT * FROM tblalert WHERE alert_id = 1"
        Dim DBCommand As New OdbcCommand(sQueryString,odbcConn)
        odbcConn.open()

        Try

            Dim odbcReader As OdbcDataReader = DBCommand.ExecuteReader(CommandBehavior.CloseConnection)
            While odbcReader.Read()
    
                xmlString += "{"
                xmlString += """alert_id"":""" & Convert.ToInt16(odbcReader("alert_id")) & ""","
                xmlString += """alert_title"":""" & Trim(odbcReader("alert_title").ToString) & ""","
                xmlString += """alert_body"":""" & Trim(odbcReader("alert_body").ToString) & ""","
                xmlString += """show_alert"":""" & Convert.ToInt16(odbcReader("show_alert")) & ""","
                xmlString += "}"

            End While

            odbcReader.Close()

        Catch ex As Exception

            odbcConn.Close()

        End Try

        odbcConn.Close()

    Catch ex As Exception

    End Try

    'xmlString = xmlString.Trim().Substring(0,xmlString.Length - 1)
    'xmlString = "[" & xmlString & "]"

    HttpContext.Current.Response.BufferOutput = True
    HttpContext.Current.Response.ContentType = "application/x-javascript"
    HttpContext.Current.Response.Write(xmlString)
    HttpContext.Current.Response.Flush()

End Sub

我的代码有什么问题?为什么“dataSource.read()”函数没有从VB文件获取数据?

解决方法

根据本网站 http://net-informations.com/q/mis/500.html 的说法,问题出在您的服务器端。有些东西没有正确配置。仔细检查您使用的连接字符串以确保它连接到正确的服务器,然后检查以确保服务器正在运行且可访问。