Search code examples
asp.netajaxresponse

Ajax response data in .net


I have a asp .net web page and a button in it. I'm calling an ajax method in the button click event as follows

 $("#btnTest").click(function () {
                 $.ajax({
                     type: 'POST',
                     url: 'test2.aspx',
                     success: function (data) {
                         alert( data);

                     },
                     error: function (data) {
                         alert("In error  ");

                     }
                 });
             });

In success part alert( data) Im getting the html code of the page test2.aspx (which one I have given in ajax url).

In test2.aspx.cs code is given as below

protected void Page_Load(object sender, EventArgs e)
    {
        jsonTest();
    }

    public List<string> jsonTest()
    {
        List<string> list = new List<string>();
        list.Add("aa");
        list.Add("bb");
        list.Add("cc");
        return list;        
    }

Why this data in 'list' is not coming as response data in ajax?


Solution

  • Try Response.Write() method for that purpose

    protected void Page_Load(object sender, EventArgs e)
    {
        Response.Write("data to be returned");
    }
    

    Or try to write static webmethods in aspx page. Those methods can be call from ajax