如何从Aspx页面发送Json数据

我尝试使用TokenInput Jquery进行多值自动完成,它需要JSON响应作为输入数据

http://loopj.com/jquery-tokeninput/

我使用ASPX页面作为源

 $(document).ready(function() { $("#txtTest").tokenInput("Complete.aspx", { theme: "facebook" }); });  

从这里编辑问题:如何以所需格式从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(); } } 

任何帮助将不胜感激。

作为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。

  

编辑:

代码隐藏 – Page_Load处理程序(JsonPage.aspx)

  string json = "[{\"name\":\"Pratik\"},{\"name\": \"Parth\"}]"; Response.Clear(); Response.ContentType = "application/json; charset=utf-8"; Response.Write(json); Response.End(); 

并通过TokenInput jQuery请求JsonPage.aspx 。 (Sample.aspx和JsonPage.aspx在同一个文件夹中)

     

你应该看看WCF。 WCF本身支持返回JSON ,您不必担心字符串连接或HTTP内容类型。