来自WebMethod的jQuery空白结果

时间:2010-12-10 11:18:11

标签: jquery asp.net-ajax webmethod

我正在尝试通过jQuery运行webmethod,它将根据一对级联dropbox的内容查询SQL数据库。我试图调用该方法并使用各种jQueries获取结果,并且在某一时刻使用UpdatePanel尝试使其工作但在每次尝试结束时我似乎都被放在同一点,所有代码都运行没有错误但是传递给最终标签文本的结果字符串是空白的。

我猜我可能错过了WebMethod中的一些非常简陋的东西,并想知道是否有人能指出我正确的方向:

数据库中的目标表(称为驱动程序)有4列:

model_id ,一个整数,应为ddlModel.SelectedItem.Value
模型,一个varchar(255),应该是ddlModel.SelectedItem.Text
驱动程序,一个整数(已尝试过但对问题没有影响,inter用于允许记录创建页面正确运行),其中包含1或0来定义if subs字段应该用在结果字符串
subs ,一个varchar(255),包含要在驱动程序为0时添加到结果字符串的数据

我没有详细说明用于填充级联下拉框的表格,因为它们似乎正常运行,但是如果需要此信息或任何其他信息,请不要犹豫。

我的代码如下:

printers.asmx.vb

Imports System.Web
Imports System.Web.Services
Imports System.Web.Services.Protocols
Imports System.Collections
Imports System.Collections.Generic
Imports System.Collections.Specialized
Imports AjaxControlToolkit
Imports System.Data
Imports System.Data.SqlClient
Imports System.Configuration

<WebService(Namespace:="http://printers.mydomainname.com/")> _
<WebServiceBinding(ConformsTo:=WsiProfiles.BasicProfile1_1)> _
<Global.Microsoft.VisualBasic.CompilerServices.DesignerGenerated()> _
<System.Web.Script.Services.ScriptService()> _
Public Class printers
Inherits System.Web.Services.WebService

<WebMethod()> _
Public Function GetMake(ByVal knownCategoryValues As String, ByVal category As String) As CascadingDropDownNameValue()
    Dim strConnection As String = ConfigurationManager.ConnectionStrings("PrinterConnection").ConnectionString
    Dim sqlConn As SqlConnection = New SqlConnection(strConnection)
    Dim strMakeQuery As String = "SELECT * FROM manufacturers ORDER BY make ASC"
    Dim cmdFetchMake As SqlCommand = New SqlCommand(strMakeQuery, sqlConn)

    Dim dtrMake As SqlDataReader
    Dim myMake As New List(Of CascadingDropDownNameValue)

    sqlConn.Open()
    dtrMake = cmdFetchMake.ExecuteReader

    While dtrMake.Read()
        Dim strMakeName As String = dtrMake("make").ToString
        Dim strMakeId As String = dtrMake("make_id").ToString

        myMake.Add(New CascadingDropDownNameValue(strMakeName, strMakeId))
    End While

    Return myMake.ToArray
End Function

<WebMethod()> _
Public Function GetModel(ByVal knownCategoryValues As String, ByVal category As String) As CascadingDropDownNameValue()
    Dim strConnection As String = ConfigurationManager.ConnectionStrings("PrinterConnection").ConnectionString
    Dim sqlConn As SqlConnection = New SqlConnection(strConnection)
    Dim strModelQuery As String = "SELECT * FROM printers WHERE make_id = @makeid"
    Dim cmdFetchModel As SqlCommand = New SqlCommand(strModelQuery, sqlConn)

    Dim dtrModel As SqlDataReader
    Dim kvModel As StringDictionary = CascadingDropDown.ParseKnownCategoryValuesString(knownCategoryValues)

    Dim intMakeId As Integer

    If Not kvModel.ContainsKey("make") Or Not Int32.TryParse(kvModel("make"), intMakeId) Then
        Return Nothing
    End If

    cmdFetchModel.Parameters.AddWithValue("@makeid", intMakeId)
    Dim myModel As New List(Of CascadingDropDownNameValue)

    sqlConn.Open()
    dtrModel = cmdFetchModel.ExecuteReader

    While dtrModel.Read()
        Dim strModelName As String = dtrModel("model").ToString
        Dim strModelId As String = dtrModel("model_id").ToString

        myModel.Add(New CascadingDropDownNameValue(strModelName, strModelId))
    End While

    Return myModel.ToArray
End Function

<WebMethod()> _
Public Function GetDriver(ByVal model As String) As String
    Dim strConnection As String = ConfigurationManager.ConnectionStrings("PrinterConnection").ConnectionString
    Dim sqlConn As SqlConnection = New SqlConnection(strConnection)
    Dim strDriverQuery As String = "SELECT * FROM drivers WHERE model = @model"
    Dim cmdFetchDriver As SqlCommand = New SqlCommand(strDriverQuery, sqlConn)

    Dim dtrDriver As SqlDataReader
    Dim intModel As Integer

    cmdFetchDriver.Parameters.AddWithValue("@model", intModel)
    Dim strResult As String = "The selected printer is"

    sqlConn.Open()
    dtrDriver = cmdFetchDriver.ExecuteReader

    dtrDriver.Read()
    Dim intDriver As Integer = dtrDriver("driver")
    Dim strSubs As String = dtrDriver("subs").ToString

    If intDriver = 1 Then
        strResult = strResult + "fully compatible with the Windows 2003 Hosted platform."
    Else
        strResult = strResult + "supported on the Windows 2003 Hosted platform via a subsituted driver:" + strSubs
    End If

    Return strResult

End Function

End Class

的Default.aspx

<%@ Page Language="VB" AutoEventWireup="false" EnableEventValidation="false" Inherits="Printer_Compatibility_Matrix_VB._Default" Codebehind="Default.aspx.vb" %>

<%@ Register Assembly="AjaxControlToolkit" Namespace="AjaxControlToolkit" TagPrefix="cc1" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml">
<head id="Head1" runat="server">

<title>Printer Compatibility Matrix</title>
<script src="jquery-1.4.4.min.js" type="text/javascript"></script>
<script language="javascript" type="text/javascript">
    function CallService() {
        $.ajax({
            type: "POST",
            url: "printers.asmx/GetDriver",
            data: $("#ddlModel option:selected").text(),
            contentType: "application/json; charset=utf-8",
            dataType: "json",
            success: OnSuccess,
            error: OnError
        });
    }

    function OnSuccess(data, status) {
        $("#lblResult").html(data.d);
    }

    function OnError(request, status, error) {
        $("#lblResult").html(request.statusText);
    }
</script>

</head>
<body>        

<form id="form1" runat="server">
<asp:ScriptManager ID="ScriptManager1" runat="server" EnablePageMethods = "true">
    <Services>
        <asp:ServiceReference Path="printers.asmx" />
    </Services>
</asp:ScriptManager>
<div>

    Manufacturer:  <asp:DropDownList ID="ddlMake" runat="server" Width="170" /><br />
    Printer:  <asp:DropDownList ID="ddlModel" runat="server" Width="170" /><br />

    <asp:Button ID="btnDriver" Text="Submit" OnClientClick="CallService(); return false;" runat="server" />
    <asp:Label ID="lblResult" Text="&nbsp;" Width="100%" runat="server" />


    <cc1:CascadingDropDown 
        id="CascadingDropDown1" 
        runat="server"
        category="Make"
        prompttext="Select a Manufacturer..."
        ServiceMethod="GetMake"
        ServicePath="printers.asmx"
        TargetControlId="ddlMake"
    />

    <cc1:CascadingDropDown 
        id="CascadingDropDown2" 
        runat="server" 
        category="Model"
        prompttext="Select a Printer..." 
        ServiceMethod="GetModel" 
        ServicePath="printers.asmx"
        TargetControlId="ddlModel"
        ParentControlId="ddlMake"
    />      
</div>
</form>
</body>
</html>

非常感谢你的时间,

吉姆

修改

看来除了Frédéric发现的错误之外,我还忘记引用jQuery,我修改了上面的代码以匹配我当前基于这些错误的迭代。

在当前状态下,它现在将“内部服务器错误”返回到标签文本中。我怀疑它与我的java有关,因为即使尝试使用此代码调用一个简单的“Hello World”,也会向标签返回一个空白结果。

1 个答案:

答案 0 :(得分:0)

我现在已经解决了这个问题,我通过传递$(“#ddlModel”)。val()而不是文本并设法使其正确响应而做了一些调整。

对于有类似问题的其他人,我修改的工作职能如下:

printers.asmx.vb

<WebMethod()> _
Public Function GetDriver(ByVal model_id As Integer)
    Dim strConnection As String = ConfigurationManager.ConnectionStrings("PrinterConnection").ConnectionString
    Dim sqlConn As SqlConnection = New SqlConnection(strConnection)
    Dim strDriverQuery As String = "SELECT * FROM drivers WHERE model_id = @model_id"
    Dim cmdFetchDriver As SqlCommand = New SqlCommand(strDriverQuery, sqlConn)

    Dim dtrDriver As SqlDataReader
    Dim intModel As Integer

    cmdFetchDriver.Parameters.AddWithValue("@model_id", intModel)
    cmdFetchDriver.Parameters("@model_id").Value = model_id

    Dim strResult As String = ""

    sqlConn.Open()
    dtrDriver = cmdFetchDriver.ExecuteReader

    dtrDriver.Read()
    Dim intDriver As Integer = dtrDriver("driver")
    Dim strSubs As String = dtrDriver("subs").ToString

    If intDriver = 1 Then
        strResult = "<font color=11CC11>The selected printer is fully compatible.</font>"
    ElseIf intDriver = 0 Then
        strResult = "<font color=FF9933>The selected printer may be compatible via the substitute print driver: " + strSubs + "</font>"
    End If

    Return strResult

End Function

Default.aspx的

<script src="jquery-1.4.4.min.js" type="text/javascript"></script>
<script language="javascript" type="text/javascript">
    function CallService() {
        $.ajax({
            type: "POST",
            url: "printers.asmx/GetDriver",
            data: "{ 'model_id': " + $("#ddlModel :selected").val() + "}",
            contentType: "application/json; charset=utf-8",
            dataType: "json",
            success: OnSuccess,
            error: OnError
        });
    }

    function OnSuccess(data, status) {
        $("#lblResult").html(data);
    }

    function OnError(request, status, error) {
        $("#lblResult").html("<font color=#FF0000>Please make a valid selection.<font>");
    }
</script>