Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
297 views
in Technique[技术] by (71.8m points)

c# - What should be the correct response from web service to display the Jquery token input results?

I am using a Jquery Token Input plugin. I have tried to fetch the data from the database instead of local data. My web service returns the json result is wrapped in xml:

 <?xml version="1.0" encoding="utf-8"?>
<string xmlns="http://tempuri.org/">[{"id":"24560","name":"emPOWERed-Admin"},{"id":"24561","name":"emPOWERed-HYD-Visitors"}]</string>

I have checked in the site http://loopj.com/jquery-tokeninput/ which says that the script should output JSON search results in the following format:

[
    {"id":"856","name":"House"},
    {"id":"1035","name":"Desperate Housewives"}
]

Both seems to be the same,but still i m not getting the items displayed in my page.

I am posting my code also. My Js code: DisplayTokenInput.js

 $(document).ready(function() {
     $("#textboxid").tokenInput('PrivateSpace.asmx/GetDl_info', {

            hintText: "Type in DL Name", theme: "facebook",
            preventDuplicates: true,
            searchDelay: 200

            });
    });

My web-service code:

[WebMethod]

    [ScriptMethod(UseHttpGet = true, ResponseFormat = ResponseFormat.Json)]
     public string GetDl_info(string q)
    {
        string dl_input = string.Empty;
        DataSet ds;
        PSData ObjDl = new PSData();
        ds = ObjDl.GetDistributionList(q);

        List<DistributionList> DLObj = new List<DistributionList>();


        foreach (DataRow datarow in ds.Tables[0].Rows)
        {
            DistributionList dl_list = new DistributionList();
            dl_list.id = Convert.ToString(datarow["id"]);
            dl_list.name = Convert.ToString(datarow["name"]);

            DLObj.Add(dl_list);
        }

        dl_input = JsonConvert.SerializeObject(DLObj);

        return dl_input;

    }

 }
public class DistributionList
    {
        public string id { get; set; }
        public string name { get; set; }
    }

I am posting the head portion of aspx code to show the library files i have included:

<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
  <title>Untitled Page</title>
  <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />

   <link href="../Styles/jquery-ui-1.8.20.custom.css" rel="stylesheet" type="text/css" />
      <link href="../Styles/token-input.css" rel="stylesheet" type="text/css" />

    <link href="../Styles/token-input-facebook.css" rel="stylesheet" type="text/css" />

    <script src="Scripts/Lib/jquery-1.7.2.min.js" type="text/javascript"></script>  

    <script src="../Scripts/jquery.tokeninput.js" type="text/javascript"></script>--%>

    <script src="DisplayTokenInput.js" type="text/javascript"></script>
<head>
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

You need to make sure that your request is a POST request. Not a get request. See this answer to find out more about why: How to let an ASMX file output JSON


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

1.4m articles

1.4m replys

5 comments

57.0k users

...