自动完成json问题

时间:2012-08-03 14:57:47

标签: asp.net json

我的网络服务方法如下:

 <WebMethod()> _
    Public Function TravelMeansList(ByVal prefix As String) As List(Of HRM_travellingMeans)
        Dim ctx As New HRMEntities
        Dim query = From c In ctx.HRM_travellingMeans Where c.name.ToUpper.StartsWith(prefix) Select c

        If query.Count > 0 Then
            Return query.ToList()
        Else
            Return Nothing
        End If

    End Function

和我的脚本如下

<script type="text/javascript">
     $(document).ready(function () {

         $("#<%=travelmeansTextBox.ClientID %>").autocomplete({


             source: function (request, response) {
                 $.ajax({
                     url: '<%=ResolveUrl("../../services/ApplicationService.asmx/TravelMeansList") %>',
                     data: "{ 'prefix': '" + request.term + "'}",
                     dataType: "json",
                     type: "POST",
                     contentType: "application/json; charset=utf-8",
                     dataFilter: function (data) { return data; },
                     success: function (data) {
                         response($.map(data.d, function (item) {
                             return Json({

                                    value: item.name
                             })
                         }))
                     },
                     error: function (response) {
                         alert(response.responseText);
                     },
                     failure: function (response) {
                         alert(response.responseText);
                     }
                 });
             },
             select: function (e, i) {

             },
             minLength: 1
         });




     });
    </script>

我的问题是,无论何时我尝试在文本框中键入任何字母,我都会收到此错误

在System.Web.Script.Serialization.JavaScriptSerializer.SerializeValueInternal(Object o, StringBuilder sb,Int32 depth,Hashtable objectsInUse,SerializationFormat serializationFormat,MemberInfo currentMember)\ r \ n在System.Web.Script.Serialization.JavaScriptSerializer.SerializeValue(Object o,StringBuilder sb,Int32 depth,Hashtable objectsInUse,SerializationFormat serializationFormat,MemberInfo currentMember) \ r \ n在System.Web.Script.Serialization.JavaScriptSerializer.Serialize.VavaScript.Serialization.VavaScript.SerializeVerialueInternal(Object)中的System.Web.Script.Serialization.JavaScriptSerializer.SerializeCustomObject(Object o,StringBuilder sb,Int32 depth,Hashtable objectsInUse,SerializationFormat serializationFormat)\ r \ n o,StringBuilder sb,Int32 depth,Hashtable objectsInUse,SerializationFormat serializationFormat,MemberInfo currentMember)\ r \ n在System.Web.Script.S中erialization.JavaScriptSerializer.SerializeValue(Object o,StringBuilder sb,Int32 depth,Hashtable objectsInUse,SerializationFormat serializationFormat,MemberInfo currentMember)\ r \ n在System.Web.Script.Serialization.JavaScriptSerializer.SerializeEnumerable ......

我做错了什么。

1 个答案:

答案 0 :(得分:1)

问题可能在于Entity Framework对象的JSON序列化。参考这篇文章:

http://mytechworld.officeacuity.com/index.php/2010/02/serializing-entity-framework-objects-into-json-using-asp-net-mvc/

作者在此建议您使用JSONResult对象,如果您使用的是ASP.NET MVC,但我不确定jQuery自动完成的效果如何。