通过自动完成 JQuery 使用 WebService 的 500(内部服务器错误)

Posted

技术标签:

【中文标题】通过自动完成 JQuery 使用 WebService 的 500(内部服务器错误)【英文标题】:500 (Internal Server Error) with WebService through AutoComplete JQuery 【发布时间】:2013-08-27 07:19:50 【问题描述】:

我正在使用 Jquery 处理一个简单的自动完成功能。

这是 Java 脚本代码

    $(document).ready(function () 
        $("#AutoCompleteText").autocomplete(
            source: function (request, response) 
                $.ajax(
                    type: "POST",
                    contentType: "application/json; charset=utf-8",
                    url: "/Service/AutoHelp.asmx/CustomerList",
                    dataType: "json",
                    data: "",
                    success: function (data) 
                        response($.map(data.d, function (item) 
                            return 
                                label: item.TEXT + '(' + item.ID + ')',
                                value: item.ID,
                                name: item.TEXT
                            
                        ))
                    ,
                    error: function (XMLHttpRequest, textStatus, errorThrown) 
                        console.log("In The ERROR");
                        console.log(XMLHttpRequest);
                        console.log(textStatus);
                        console.log(errorThrown);
                    
                );
            ,
            minLength: 1
        );
    );

这是网络服务代码

Imports System.Web.Services
Imports System.Web.Services.Protocols
Imports System.ComponentModel
Imports System.Web.Script.Services

' To allow this Web Service to be called from script, using ASP.NET AJAX, uncomment the following line.
' <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 AutoHelp
    Inherits System.Web.Services.WebService

    <WebMethod()> _
    Public Function HelloWorld() As String
        Return "Hello World"
    End Function

    Public Class dbHelpData
        Dim _id As String
        Dim _text As String

        Public Property ID As String
            Get
                Return _id
            End Get
            Set(ByVal value As String)
                _id = value
            End Set
        End Property

        Public Property TEXT As String
            Get
                Return _text
            End Get
            Set(ByVal value As String)
                _text = value
            End Set
        End Property

    End Class

    <WebMethod()> _
    <ScriptMethod(ResponseFormat:=ResponseFormat.Json)> _
    Public Function CustomerList() As List(Of dbHelpData)
        Dim CustList As New List(Of dbHelpData)

        Dim mCustList As dbHelpData
        mCustList = New dbHelpData
        mCustList.ID = "1"
        mCustList.TEXT = "Kartik"
        CustList.Add(mCustList)

        mCustList = New dbHelpData
        mCustList.ID = "2"
        mCustList.TEXT = "Sarika"
        CustList.Add(mCustList)

        mCustList = New dbHelpData
        mCustList.ID = "3"
        mCustList.TEXT = "Yashika"
        CustList.Add(mCustList)

        Return CustList
    End Function

End Class

当我尝试执行自动填充时,它给了我一个错误 POST http://ab99.pricecompareindia.com/Service/AutoHelp.asmx/CustomerList 500(内部服务器错误) 但我尝试直接从浏览器执行服务,它给了我一个 XML 输出视图,如下所示。

<ArrayOfDbHelpData xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"     xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns="http://tempuri.org/">
    <dbHelpData>
        <ID>1</ID>
        <TEXT>Kartik</TEXT>
    </dbHelpData>
    <dbHelpData>
        <ID>2</ID>
        <TEXT>Sarika</TEXT>
    </dbHelpData>
    <dbHelpData>
        <ID>3</ID>
        <TEXT>Yashika</TEXT>
    </dbHelpData>
</ArrayOfDbHelpData>

我很难弄清楚我做错了什么。完整代码在http://ab99.pricecompareindia.com/运行

谁能帮帮我。 提前致谢。

【问题讨论】:

500 表示服务器错误。从查看服务器日志开始 - 你应该在那里找到答案。 如果可能的话,尝试调试web服务,实际错误只有那里。 ' To allow this Web Service to be called from script, using ASP.NET AJAX, uncomment the following line. 你这样做了吗? // 取消注释这一行 thx.. 取消注释是正确的解决方案.. 我应该仔细查看说明,谢谢您的帮助 VISH 【参考方案1】:

将此行放在您的 ajax 中: 数据:'“前缀”:“'+ request.term +'”', 它会和我一样工作。

【讨论】:

以上是关于通过自动完成 JQuery 使用 WebService 的 500(内部服务器错误)的主要内容,如果未能解决你的问题,请参考以下文章