繁体   English   中英

在Asp.Net中获取jQuery Ajax返回数据

[英]getting jQuery Ajax return data in Asp.Net

我是jQuery的新手,不了解jQuery Ajax如何返回数据。 我有一些简单的功能来获取如下数据

[WebMethod(EnableSession = false)]
protected int SignIn()
{
    return 0;
}

在我的.aspx页面中,我有这个

$(document).ready(function () {
        $("#si").click(function 
            () {
            $.ajax({
                type: "POST",
                url: "SignIn.aspx/SignIn",
                contentType: "application/json",
                success: function (txt) {
                    alert(txt);
                }
            });
        });
    });

但在警报中,我得到了整个SignIn.aspx(所有html标记等)。 如何提醒SignIn()返回的0?

SignIn方法设为静态和公共,并使用以下代码显示警报: alert(txt.d);

试试这个样本。 我已经将id从aspx传递给了处理程序,并刚刚从那里返回以再次将服务器端数据显示给aspx

这是示例示例.....

处理程序代码:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;

namespace TestProject
{
    /// <summary>
    /// Summary description for TestHandler1
    /// </summary>
    public class TestHandler1 : IHttpHandler
    {

        public void ProcessRequest(HttpContext context)
        {
            string id = context.Request["id"];
            context.Response.ContentType = "text/plain";
            context.Response.Write(id);
        }

        public bool IsReusable
        {
            get
            {
                return false;
            }
        }
    }
}

和aspx

    <html xmlns="http://www.w3.org/1999/xhtml">
<head>
    <title></title>
    <script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.4.2/jquery.min.js"></script>
  </head>
<body>
    <form id="form1" runat="server">

    </form>
    <script type="text/javascript">
        $(function () {
            $.ajax({
                type: 'POST',
                url: 'TestHandler.ashx?id='+ 1,
                data: 'id=' + 1,
                success: function (msg) {
                    alert(msg);
                }
            });

        });
    </script>
</body>
</html>

您正在向ASPX文件索取数据,我认为应该是ASMX。

查阅Dave Ward的文章,我在其中了解了所有这些信息: http : //encosia.com/asp-net-web-services-mistake-manual-json-serialization/

我可以做的最简单的示例如下所示:

添加包含以下内容的Web服务(ASMX)

using System.Web.Services;

[WebService(Namespace = "http://tempuri.org/")]
[WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]
[System.Web.Script.Services.ScriptService]
public class WebService : System.Web.Services.WebService {
   [WebMethod]
   public int Status(int input) {
      return input + 1;
   }
}

然后在您的HTML中

<html>
<head>
    <title>Test</title>
    <script src="jquery-1.6.2.min.js" ></script>
</head>
<body>
<script>
  $(function () {
    $.ajax({
      url: 'WebService.asmx/Status',
      data: '{ input: 0 }',
      type: 'POST',
      dataType: 'json',
      contentType: 'application/json',
      success: function (data, status) {
        alert(data);
        alert(typeof data);
      }
    });
  });
</script>
</body>
</html>

在ajax调用中,data中定义的字符串是web方法的输入。 名称必须匹配。 在成功回调中,第一个警报显示返回的值(输入加一个),第二个警报显示它是一个数字,而不是字符串。 由于数据类型设置为JSON,因此Web方法返回JSON,从而可以正确键入数据。

希望这可以帮助。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM