在Asp.Net让jQuery的阿贾克斯返回数据 [英] getting jQuery Ajax return data in Asp.Net

查看:185
本文介绍了在Asp.Net让jQuery的阿贾克斯返回数据的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在jQuery的新手,不明白怎么jQuery中的Ajax返回的数据。我有一些简单的函数来得到像下面的一些数据

I'm a newbie in jQuery and don't understand how in jQuery Ajax returns data. I have some simple function to get some data like below

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

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

and in my .aspx page I have this

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

但在警报,我得到了整个SignIn.aspx(HTML标签等)。如何提醒0其中签到()返回?谢谢

but in alert I get the whole SignIn.aspx (all html tags and so on). how to alert the 0 which the SignIn() returns?thanks

推荐答案

您所要求的数据的ASPX文件,我认为这应该是一个ASMX。

You are asking an ASPX file for data and I think that should be an ASMX.

看看戴夫病房的帖子里我靠在这一切:的 http://encosia.com/asp-net-web-services-mistake-manual-json-serialization/

Check out Dave Ward's post where I leaned about all this: http://encosia.com/asp-net-web-services-mistake-manual-json-serialization/

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

The simplest example I could make looks like this:

添加包含一个Web服务(ASMX)

Add a Web Service (ASMX) containing

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做

Then in your HTML do

<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调用数据定义的字符串是输入的Web方法。名称必须匹配。在成功的回调,第一警报显示返回(输入加1)的值和所述第二警报表明,它是一个数字,而不是字符串。由于该数据类型设置为JSON,Web方法返回JSON允许数据被输入正确。

In the ajax call the string defined in data is the input to the web method. Names must match. In the success callback, the first alert shows the value returned (input plus one) and the second alert shows that it is a number, not a string. Because the datatype is set to JSON, the web method returns JSON allowing the data to be typed correctly.

希望这有助于。

这篇关于在Asp.Net让jQuery的阿贾克斯返回数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆