如何从ASPX页面发送JSON数据 [英] How to send Json Data from Aspx page
问题描述
我试图用TokenInput Jquery的多值自动完成它需要JSON响应作为输入数据
I tried to use TokenInput Jquery for multiple value autocomplete where it requires the JSON response as input data
http://loopj.com/jquery-tokeninput/
我使用的ASPX页面源
I am using ASPX page as source
<script type="text/javascript" >
$(document).ready(function() {
$("#txtTest").tokenInput("Complete.aspx", {
theme: "facebook"
});
});
</script>
编辑从这里
问:如何根据查询字符串从Complete.aspx提供从aspx页面的JSON数据所需的格式,因为我有数据表与值
Edited From Here Question: How to provide the JSON data from a aspx page in the desired format as i have datatable with values according to Querystring from 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();
}
}
任何帮助将AP preciated。
Any help will be appreciated.
推荐答案
替代 WCF
,您可以创建的WebMethod
中的.aspx。
Alternative to the WCF
, you can create WebMethod
in .aspx.
[WebMethod]
public static string Info()
{
JavaScriptSerializer js = new JavaScriptSerializer();
string result = js.Serialize(new string[] { "one", "two", "three" });
return result;
}
和要求通过Ajax调用此的WebMethod。
and request this WebMethod via Ajax call.
<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>
编辑:
code-背后 - 的Page_Load处理器(JsonPage.aspx)
Code-behind - Page_Load handler (JsonPage.aspx)
string json = "[{\"name\":\"Pratik\"},{\"name\": \"Parth\"}]";
Response.Clear();
Response.ContentType = "application/json; charset=utf-8";
Response.Write(json);
Response.End();
,并通过要求 JsonPage.aspx
的 TokenInput 的的jQuery
。 (Sample.aspx&安培; JsonPage.aspx都在同一个文件夹中)
and request the JsonPage.aspx
via TokenInput jQuery
. (Sample.aspx & JsonPage.aspx are in same folder)
<script type="text/javascript">
$(function () {
$("#txt1").tokenInput("JsonPage.aspx");
});
</script>
<body>
<input type="text" id="txt1"/>
</body>
这篇关于如何从ASPX页面发送JSON数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!