不一致的错误:集合被修改枚举操作可能无法执行

时间:2017-11-29 02:50:33

标签: javascript c# .net serialization

我有一个连接到Web服务到数据库的网站

我有这个不一致的错误,上面写着

  

{"消息":"收集已修改;枚举操作可能无法执行。"," StackTrace":" at System.ThrowHelper.ThrowInvalidOperationException(ExceptionResource resource)\ r \ n在System.Collections.Generic.List 1.Enumerator.MoveNextRare()\r\n at System.Collections.Generic.List 1.Enumerator.MoveNext()\ r \ n在System.Web.Script.Serialization.JavaScriptSerializer.SerializeEnumerable (IEnumerable可枚举,StringBuilder sb,Int32深度,Hashtable objectsInUse,SerializationFormat serializationFormat)\ r \ n在System.Web.Script.Serialization.JavaScriptSerializer.SerializeValueInternal(Object o,StringBuilder sb,Int32 depth,Hashtable objectsInUse,SerializationFormat serializationFormat,MemberInfo currentMember) )\\ n \ n \ n \ n \ n \ n \ n \ n \ n \ n \ n \ n \ nSystem.Web.Script.Serial。 .Serialize(Object obj,Str

我在c#web服务中有这些代码,此函数可能返回0行或更多行。

public class Liked
    {
        public bool liked;
    }

static List<Liked> _get_liked = new List<Liked> { };
    [ScriptMethod(ResponseFormat = ResponseFormat.Json)]
    [WebMethod]
    public List<Liked> get_liked(string userID,string postID)
    {
        DataTable table = null;
        SqlCommand cmd = new SqlCommand();

        cmd.CommandText = "Like_Retrieve";

        cmd.Parameters.AddWithValue("@UserId", userID);
        cmd.Parameters.AddWithValue("@PostId", postID);

        cmd.CommandType = System.Data.CommandType.StoredProcedure;
        table = this.dbcon.ExecuteDataTable(cmd);

        _get_liked.Clear();
        Liked _list = new Liked();
        if (table.Rows.Count > 0)
        {            
            _list.liked = true;      
        }
        else
        {
            _list.liked = false;
        }
        _get_liked.Add(_list);
        return _get_liked;
    }

错误来源不一致。我在我的javascript中有这些

function checkLike(userId, postId) {
    $.ajax({

        type: "POST",
        url: "../Main.asmx/get_liked",
        data: "{'userID':'" + userId + "', 'postID':'" + postId + "'}",
        contentType: "application/json; charset=utf-8",
        dataType: "json",
        success: function (response) {
            var result = response.d;
            $.each(result, function (index,data) {
                var liked = data.liked;
                if (liked == true) {
                    $("#" + postId).prev().parent("button").css("background", "#ccc");
                }
                else
                {
                    $("#" + postId).prev().parent("button").css("background", "white");
                }
            });

        },
        error: function (xhr, ajaxOptions, thrownError) {
            alert('error');
            console.log(postId);
            console.log(userId);
            console.log(xhr.responseText);
            console.log(thrownError);
        }

    });
}

我希望有人会帮助我。我现在已经敲了好几个小时

0 个答案:

没有答案