什么应该从网络服务的正确响应显示Jquery的记号输入的结果吗? [英] What should be the correct response from web service to display the Jquery token input results?
问题描述
我使用一个jQuery令牌输入插件。我试图从数据库中,而不是本地数据读取数据。我的Web服务返回JSON结果被包裹在XML:
<?XML版本=1.0编码=UTF-8&GT?;
&LT;字符串xmlns=\"http://tempuri.org/\">[{\"id\":\"24560\",\"name\":\"emPOWERed-Admin\"},{\"id\":\"24561\",\"name\":\"emPOWERed-HYD-Visitors\"}]</string>
我在网站 http://loopj.com/jquery-tokeninput/ 已经检查了这说脚本应该按以下格式输出JSON搜索结果:
[
{ID:856,名:豪斯医生},
{ID:1035,名:绝望主妇}
]
这两个似乎是相同的,但我仍然很老在我的网页显示的项目。
我张贴我的code也。
我的js code:DisplayTokenInput.js
$(文件)。就绪(函数(){
$(#textboxid)。tokenInput('PrivateSpace.asmx / GetDl_info',{ hintText:键入DL名称,主题为:脸谱,
preventDuplicates:真实,
searchDelay:200 });
});
我的Web服务code:
[的WebMethod] [ScriptMethod(UseHttpGet = TRUE,ResponseFormat = ResponseFormat.Json)
公共字符串GetDl_info(串Q)
{
字符串dl_input =的String.Empty;
数据集DS;
PSData ObjDl =新PSData();
DS = ObjDl.GetDistributionList(Q); 清单&LT; DistributionList&GT; DLObj =新的List&LT; DistributionList&GT;();
的foreach(DataRow的数据行的ds.Tables [0] .Rows)
{
DistributionList dl_list =新DistributionList();
dl_list.id = Convert.ToString(数据行[ID]);
dl_list.name = Convert.ToString(数据行[名称]); DLObj.Add(dl_list);
} dl_input = JsonConvert.SerializeObject(DLObj); 返回dl_input; } }
公共类DistributionList
{
公共字符串ID {搞定;组; }
公共字符串名称{;组; }
}
我张贴的aspx code的头部,以显示我已经包括库文件:
&LT; HTML的xmlns =http://www.w3.org/1999/xhtml&GT;
&LT;头=服务器&GT;
&LT;标题&GT;无标题页&LT; /标题&GT;
&LT; META HTTP-EQUIV =Content-Type的CONTENT =text / html的;字符集= UTF-8/&GT; &LT;链接的href =../样式/ jQuery的-UI-1.8.20.custom.css的rel =stylesheet属性类型=文/ CSS/&GT;
&LT;链接的href =../样式/令牌input.css的rel =stylesheet属性类型=文/ CSS/&GT; &LT;链接的href =../样式/令牌输入facebook.css的rel =stylesheet属性类型=文/ CSS/&GT; &LT;脚本SRC =脚本/库/ jQuery的-1.7.2.min.js类型=文/ JavaScript的&GT;&LT; / SCRIPT&GT; &LT; SCRIPT SRC =../脚本/ jquery.tokeninput.js类型=文/ JavaScript的&GT;&LT; / SCRIPT&GT; - %GT; &LT;脚本SRC =DisplayTokenInput.js类型=文/ JavaScript的&GT;&LT; / SCRIPT&GT;
&LT; HEAD&GT;
我会假设code的插件未设置内容类型为Ajax请求的JSON,所以你可以之前自己做
:用$ .ajaxSetup即服务电话 $。ajaxSetup({
的contentType:应用/ JSON的;字符集= UTF-8
});
更新:显然,ASMX服务有时会有问题的字符集= UTF-8部分,因此,如果不工作,你可以尝试只是应用/ JSON的
更新2
我不认为这是contentType中引起问题,请使用以下强制为Ajax请求的POST,看看这个修复它:
$。ajaxSetup({
类型:POST,则contentType:应用/ JSON的;字符集= UTF-8
});
更新3
有一个默认插件里面设置你使用,可以改变从GET张贴请求。看到这里它的GitHub库: jquery.tokeninput.js
和在你的项目中的js文件副本,更改行:
VAR DEFAULT_SETTINGS = {
//搜索设置
方法:GET,
到
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.
这篇关于什么应该从网络服务的正确响应显示Jquery的记号输入的结果吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!