Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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?

like image 721
Sudha Avatar asked Apr 18 '13 11:04

Sudha


3 Answers

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

like image 64
Anoop Joshi P Avatar answered Sep 27 '22 21:09

Anoop Joshi P


You cannot return data from aspx to ajax. try this

HttpContext.Current.Response.Write(list);
HttpContext.Current.Response.Flush();
HttpContext.Current.ApplicationInstance.CompleteRequest();
HttpContext.Current.Response.SuppressContent = true;

This will help to solve your problem.

like image 35
mmpatel009 Avatar answered Sep 27 '22 19:09

mmpatel009


You can't call a normal page method from jQuery.
You need to create a web-method to access that from jQuery.
Here is a good link regarding how to call a page method
http://encosia.com/using-jquery-to-directly-call-aspnet-ajax-page-methods/

like image 29
शेखर Avatar answered Sep 27 '22 20:09

शेखर