[英]How to send Json Data from Aspx page
I tried to use TokenInput Jquery for multiple value autocomplete where it requires the JSON response as input data 我尝试将TokenInput Jquery用于多值自动完成功能,在此它需要JSON响应作为输入数据
http://loopj.com/jquery-tokeninput/ http://loopj.com/jquery-tokeninput/
I am using ASPX page as source 我正在使用ASPX页面作为源
<script type="text/javascript" >
$(document).ready(function() {
$("#txtTest").tokenInput("Complete.aspx", {
theme: "facebook"
});
});
</script>
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 从这里编辑问题:如何从aspx页面以所需格式提供JSON数据,因为我具有根据Complete.aspx的Querystring带有值的数据表
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();
}
}
Any help will be appreciated. 任何帮助将不胜感激。
You should take a look at WCF. 您应该看看WCF。 WCF has native support for returning JSON and you don't have to worry about string concatenation or HTTP content types.
WCF具有返回JSON的本机支持,因此您不必担心字符串连接或HTTP内容类型。
Alternative to the WCF
, you can create WebMethod
in .aspx. 替代
WCF
,您可以在.aspx中创建WebMethod
。
[WebMethod]
public static string Info()
{
JavaScriptSerializer js = new JavaScriptSerializer();
string result = js.Serialize(new string[] { "one", "two", "three" });
return result;
}
and request this WebMethod via Ajax call. 并通过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>
EDIT: 编辑:
Code-behind - Page_Load handler (JsonPage.aspx) 代码隐藏-Page_Load处理程序(JsonPage.aspx)
string json = "[{\"name\":\"Pratik\"},{\"name\": \"Parth\"}]";
Response.Clear();
Response.ContentType = "application/json; charset=utf-8";
Response.Write(json);
Response.End();
and request the JsonPage.aspx
via TokenInput jQuery
. 并请求
JsonPage.aspx
经由TokenInput jQuery
。 (Sample.aspx & JsonPage.aspx are in same folder) (Sample.aspx和JsonPage.aspx在同一文件夹中)
<script type="text/javascript">
$(function () {
$("#txt1").tokenInput("JsonPage.aspx");
});
</script>
<body>
<input type="text" id="txt1"/>
</body>
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.