最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - How to send Json Data from Aspx page - Stack Overflow

programmeradmin2浏览0评论

I tried to use TokenInput Jquery for multiple value autoplete where it requires the JSON response as input data

/

I am using ASPX page as source

<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

 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.

I tried to use TokenInput Jquery for multiple value autoplete where it requires the JSON response as input data

http://loopj./jquery-tokeninput/

I am using ASPX page as source

<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

 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.

Share Improve this question edited Jul 26, 2012 at 9:13 Pratik asked Jul 23, 2012 at 13:09 PratikPratik 1,5127 gold badges21 silver badges36 bronze badges 4
  • The problem is the json data is not going back to the javascript or you want a way to build json strings? – Tomer Commented Jul 23, 2012 at 13:15
  • I am not sure on both the things as i am bind not to use firebug tools (IE8) but the data is not reaching in the JSON format to the javascript that is the main cause i think – Pratik Commented Jul 23, 2012 at 13:19
  • IE8 has its own debugger tools, accessed via F12 key. Additionally, you can use fiddler to get full details on request and response. – Mike Guthrie Commented Jul 23, 2012 at 13:25
  • @MikeGuthrie i dont have access to download any utility program on my desktop.I tried IE8 developer tools not much of help :( – Pratik Commented Jul 23, 2012 at 13:33
Add a ment  | 

2 Answers 2

Reset to default 2

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;
    }

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>

EDIT:

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();

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>

You should take a look at WCF. WCF has native support for returning JSON and you don't have to worry about string concatenation or HTTP content types.

发布评论

评论列表(0)

  1. 暂无评论