6

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?

4

4 回答 4

5

为此目的尝试 Response.Write() 方法

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

或者尝试在aspx页面中编写静态webmethods。这些方法可以从ajax调用

于 2013-04-18T11:41:26.310 回答
2

您不能将数据从 aspx 返回到 ajax。尝试这个

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

这将有助于解决您的问题。

于 2013-04-18T11:51:42.880 回答
1

您不能从 jQuery 调用正常的页面方法。
您需要创建一个web-method从 jQuery 访问它。
这是关于如何调用页面方法的一个很好的链接
http://encosia.com/using-jquery-to-directly-call-aspnet-ajax-page-methods/

于 2013-04-18T11:20:16.723 回答
0

像这样使用..

 $("#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  ");

                     }
                 });
             });
于 2013-04-18T12:39:30.880 回答