在 jQuery Tag-it UI 小部件上出现错误无法加载资源

Getting error on jQuery Tag-it UI widget Failed to load resource

我在 Asp.net 中使用 jQuery UI 小部件 TagIt。代码工作正常,但我想突出显示列表中不可用的标签。

之前我问过这个问题,用的是同样的方法

我的密码是

.aspx代码

<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.10.2/jquery.min.js" type="text/javascript" charset="utf-8"></script>
<link href="../CSS/jquery.tagit.css" rel="stylesheet" />
        <script src="https://ajax.googleapis.com/ajax/libs/jqueryui/1.9.2/jquery-ui.min.js" type="text/javascript" charset="utf-8"></script>
          <script src="../JavaScript/tag-it.js"></script>
        <link href="../CSS/tagit.ui-zendesk.css" rel="stylesheet" />
    <script>
    $(function () {
        $.ajax({
            url: 'UpdateSingImgKwds.aspx/GetKeywords',
            type: 'GET',
            datatype: "json",
            contentType: "application/json; charset=utf-8",
            success: function (res) {

                console.log(res.d);


                $('#singleFieldTags').tagit({
                    caseSensitive: false,
                    availableTags: res.d,
                    allowSpaces: true,
                    singleField: true,
                    singleFieldNode: $('#txtCompKwds'),
                    beforeTagAdded: function (event, ui) {
                        if ((res.d).indexOf(ui.tagLabel.toLowerCase()) == -1) {
                            $(ui.tag).css('background', '#F9999A')
                        }
                    }

                });



            },
            failure: function (err) {
                alert(err);
            }
        });   


    });

CS 码

   [WebMethod]
    [ScriptMethod(UseHttpGet = true, ResponseFormat = ResponseFormat.Json)]
    public static string[] GetKeywords()
    {
        List<string> lst = new List<string>();
        string queryString = "select Keyword from SIB_KWD_Library";
        using (SqlConnection connection = new SqlConnection(ConfigurationManager.AppSettings["vConnString"].ToString()))
        {
            using (SqlCommand command = new SqlCommand(queryString, connection))
            {
                connection.Open();
                using (SqlDataReader reader = command.ExecuteReader())
                {

                    while (reader.Read())
                    {
                        lst.Add(reader["Keyword"].ToString());
                    }
                }
            }
        }
        lst = lst.ConvertAll(d => d.ToLower());
        return lst.ToArray();
    }

当数组列表的长度为 Up-to 7345 时出现错误 resource: the server responded with a status of 500 (Internal Server Error)

JS Fiddle

遇到问题请帮忙。

这可能是 JSON length limitation 的缘故,只需将其添加到您的网络配置中即可试一试

<configuration>
<system.web.extensions>
    <scripting>
        <webServices> 
            <jsonSerialization maxJsonLength="50000000"/> 
        </webServices> 
    </scripting> 
</system.web.extensions> 
</configuration>

Okay the problem was with the length of data you were sending back as result and when traced I used to get below error message in console:

Error during serialization or deserialization using the JSON JavaScriptSerializer. The length of the string exceeds the value set on the maxJsonLength property.

So you need to make a setting in your web.config file to allow maxJsonLength for jsonSerialization like one below:

<system.web.extensions>
    <scripting>
      <webServices>
        <jsonSerialization maxJsonLength="2147483644"/> //This is the max value for integer
      </webServices>
    </scripting>
</system.web.extensions>

and I would like to make few changes to your webmethod and the way of loading source as below:

Webmethod

[WebMethod]
[ScriptMethod(UseHttpGet = true, ResponseFormat = ResponseFormat.Json)]
public static List<string> GetKeywords()
{
    List<string> lst = new List<string>();
    string queryString = "select Keyword from SIB_KWD_Library";
    using (SqlConnection connection = new SqlConnection(ConfigurationManager.AppSettings["vConnString"].ToString()))
    {
        connection.Open();
        using (SqlCommand command = new SqlCommand(queryString, connection))
        {

            using (SqlDataReader reader = command.ExecuteReader())
            {

                while (reader.Read())
                {
                    lst.Add(reader["Keyword"].ToString());
                }
            }
        }
    }
    lst = lst.ConvertAll(d => d.ToLower());
    return lst; //return it as list itself
}

Now the way you call the ajax source:

$(document).ready(function () {
     var source = []; //declare a source array
     $.when(//get the source first
            $.ajax({
                url: '<%= ResolveUrl("Default.aspx/GetKeywords") %>',
                type: 'GET',
                datatype: "json",
                contentType: "application/json; charset=utf-8",
                success: function (res) {
                    source = res.d;
                },
                error: function (jqXHR, textStatus, errorThrown) {
                    console.log(jqXHR);
                    console.log(textStatus);
                    console.log(errorThrown);
                }
            })
     ).done(function () { //once done assign it as you cannot directly assign the source as availableTags:res.d
            $('#mySingleFieldTags').tagit({
                  caseSensitive: false,
                  availableTags: source,
                  allowSpaces: true,
                  singleField: true,
                  singleFieldNode: $('#txtCompKwds'),
                  beforeTagAdded: function (event, ui) {
                       console.log(source);
                       if ((source).indexOf(ui.tagLabel.toLowerCase()) == -1) {
                            $(ui.tag).css('background', '#F9999A')
                        }
                  }
             });
     });
});

Check the console once everything is done. You will have the array of values.

Note : To check the error just remove the above setting in web.config file and keep all the other codes mentioned and check the console once done!!