Web 服务显示 Jquery 令牌输入结果的正确响应应该是什么? [英] What should be the correct response from web service to display the Jquery token input results?

查看:33
本文介绍了Web 服务显示 Jquery 令牌输入结果的正确响应应该是什么?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用 Jquery 令牌输入插件.我试图从数据库而不是本地数据中获取数据.我的 Web 服务返回 json 结果包装在 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>

我已经检查了网站 http://loopj.com/jquery-tokeninput/表示该脚本应按以下格式输出 JSON 搜索结果:

<预><代码>[{"id":"856","name":"House"},{"id":"1035","name":"绝望的主妇"}]

两者似乎相同,但我仍然没有在我的页面中显示项目.

我也发布了我的代码.我的js代码:DisplayTokenInput.js

 $(document).ready(function() {$("#textboxid").tokenInput('PrivateSpace.asmx/GetDl_info', {提示文本:输入 DL 名称",主题:facebook",防止重复:真,搜索延迟:200});});

我的网络服务代码:

[WebMethod][ScriptMethod(UseHttpGet = true, ResponseFormat = ResponseFormat.Json)]公共字符串 GetDl_info(string q){字符串 dl_input = string.Empty;数据集 ds;PSData ObjDl = new PSData();ds = ObjDl.GetDistributionList(q);列表<分发列表>DLObj = new List();foreach(ds.Tables[0].Rows 中的 DataRow 数据行){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);返回 dl_input;}}公共类分发列表{公共字符串 ID { 获取;放;}公共字符串名称 { 获取;放;}}

我发布了 aspx 代码的头部部分以显示我包含的库文件:

<head runat="服务器"><title>无标题页</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><头>

解决方案

我认为插件的代码没有将 ajax 请求的内容类型设置为 JSON,因此您可以在服务调用之前自己完成使用 $.ajaxSetup 即:

$.ajaxSetup({内容类型:应用程序/json;字符集=utf-8"});

更新:显然 asmx 服务有时会遇到charset=utf-8"部分的问题,因此如果这不起作用,您可以尝试使用application/json"

更新 2:

我不认为是 contentType 导致了问题,请使用以下内容对 ajax 请求强制 POST 并查看是否可以解决问题:

$.ajaxSetup({类型:POST",内容类型:应用程序/json;字符集=utf-8"});

更新 3:

您使用的插件中有一个默认设置,可以将请求从 GET 更改为 POST.在它的 GitHub 存储库上看到这里:jquery.tokeninput.js

在项目中 js 文件的副本中,更改行:

var DEFAULT_SETTINGS = {//搜索设置方法:获取",

var DEFAULT_SETTINGS = {//搜索设置方法:POST",

我还假设插件以忽略全局 jquery ajax 设置的方式构造查询,因此您不需要再包含我之前的代码片段.

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>

解决方案

I would assume that the code for the plugin isn't setting the content-type for ajax requests to JSON, so you could do it yourself before the service call with $.ajaxSetup ie:

$.ajaxSetup({
  contentType: "application/json; charset=utf-8"
});

UPDATE: Apparently asmx services sometimes have issues with the 'charset=utf-8' portion, so if that doesn't work you could try just 'application/json'

UPDATE 2:

I don't think it's the contentType causing the issue, use the following to force a POST for ajax requests and see if this fixes it:

$.ajaxSetup({
  type: "POST", contentType: "application/json; charset=utf-8"
});

UPDATE 3:

There is a default setting inside the plugin you're using that can change the requests from GET to POST. See here on it's GitHub repo: jquery.tokeninput.js

and in your copy of the js file in the project, change the line:

var DEFAULT_SETTINGS = {
    // Search settings
    method: "GET",

to

var DEFAULT_SETTINGS = {
    // Search settings
    method: "POST",

I also assume that the plugin constructs the query in a way that ignores the global jquery ajax settings anyway, so you shouldn't need to include my earlier snippets anymore.

这篇关于Web 服务显示 Jquery 令牌输入结果的正确响应应该是什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆