Ajax response data in .net

I have an asp.net webpage and a button. I call the 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 the successful notification of the part (data) Im gets the html code of the page test2.aspx (which I gave in the ajax url).

The code test2.aspx.cs is listed 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 does this data in the β€œlist” not come as response data in ajax?

+6
source share
4 answers

Try the Response.Write () method for this purpose

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

Or try writing static web methods on an aspx page. These methods can be called from ajax

+5
source

You cannot return data from aspx to ajax. try it

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

This will help solve your problem.

+2
source

You cannot call a regular page method from jQuery.
You need to create a web-method to access this from jQuery.
Here is a good link on how to call the page method
http://encosia.com/using-jquery-to-directly-call-aspnet-ajax-page-methods/

+1
source

Use like this ..

  $("#btnTest").click(function () { $.ajax({ type: 'POST', url: 'test2.aspx', success: function (data) { alert( data.d[0]); alert( data.d[1]); alert( data.d[2]); }, error: function (data) { alert("In error "); } }); }); 
0
source

All Articles