我尝试使用 TokenInput Jquery 进行多值自动完成,它需要 JSON 响应作为输入数据
http://loopj.com/jquery-tokeninput/
我使用 ASPX 页面作为源
<script type="text/javascript" >
$(document).ready(function() {
$("#txtTest").tokenInput("Complete.aspx", {
theme: "facebook"
});
});
</script>
从此处编辑 问题:如何以所需的格式从 aspx 页面提供 JSON 数据,因为我有数据表,其值根据来自 Complete.aspx 的查询字符串
protected void Page_Load(object sender, EventArgs e)
{
if (!string.IsNullOrEmpty(Request.QueryString["q"]))
{
string json = "[{\"Id\":\"1\",\"name\": \"Test 1\"},{\"Id\":\"2\",\"name\": \"Test 2\"}]";
Response.Clear();
Response.ContentType = "application/json; charset=utf-8";
Response.Write(json);
Response.End();
}
}
任何帮助将不胜感激。
最佳答案
除了 WCF
,您还可以在 .aspx 中创建 WebMethod
。
[WebMethod]
public static string Info()
{
JavaScriptSerializer js = new JavaScriptSerializer();
string result = js.Serialize(new string[] { "one", "two", "three" });
return result;
}
并通过 Ajax 调用请求此 WebMethod。
<script type="text/javascript">
$(function () {
$("#button1").click(function () {
$.ajax({
url: "Default.aspx/Info",
data: "{}",
contentType: "application/json",
success: function (data) {
alert(data.d);
},
type: "post",
dataType : "json"
});
});
});
</script>
编辑:
代码隐藏 - Page_Load 处理程序 (JsonPage.aspx)
string json = "[{\"name\":\"Pratik\"},{\"name\": \"Parth\"}]";
Response.Clear();
Response.ContentType = "application/json; charset=utf-8";
Response.Write(json);
Response.End();
并通过TokenInputjQuery
请求JsonPage.aspx
。 (Sample.aspx 和 JsonPage.aspx 位于同一文件夹中)
<script type="text/javascript">
$(function () {
$("#txt1").tokenInput("JsonPage.aspx");
});
</script>
<body>
<input type="text" id="txt1"/>
</body>
关于javascript - 如何从Aspx页面发送Json数据,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11613194/