ASP.NET是jQuery的ajax函数的一个问题

时间:2010-07-28 08:55:22

标签: c# asp.net jquery

我有按钮和jQuery脚本(启动进度条):

<script src="../_Files/JScripts/jquery-1.3.2.js" type="text/javascript"></script>
<script src="../_Files/JScripts/jquery-ui-1.7.2.custom.min.js" type="text/javascript"></script>

 var intervalID;
 $("#<%=this.Button1.ClientID%>").click(
            function() {

                intervalID = setInterval(updateProgress, 500);

                $.ajax({
                    type: "POST",
                    url: "CustomerImport.aspx/ExecuteImport",
                    data: "{}",
                    contentType: "application/json; charset=utf-8",
                    dataType: "json",
                    async: true,
                    success: function()
                    {
                        $("#progressbar").progressbar("value", 100);
                        clearInterval(intervalID);
                        $("#result").text('ok');
                    }
                });

                return false;
            }
        );

   function updateProgress() {

            $.ajax({
                type: "POST",
                url: "CustomerImport.aspx/GetProgress",
                data: "{}",
                contentType: "application/json; charset=utf-8",
                dataType: "json",
                async: true,
                success: function(msg) {
                    $("#result").text = msg.d;
                    var value = $("#progressbar").progressbar("option", "value");
                    if (value < 100) {
                        $("#progressbar").progressbar("value", msg.d);
                        $("#result").text(msg.d);
                    }
                    else {
                        clearInterval(intervalID);
                        window.location = window.location;
                    }
                }
            });
        }

方法:

    [System.Web.Services.WebMethod]
    public void ExecuteImport()
    {
        _Presenter.ExecuteImport();
    }

问题是,该方法未被调用。为什么?

当我替换警告显示的例如$.ajax的{​​{1}}时,它会正常工作

3 个答案:

答案 0 :(得分:3)

您是否使用[ScriptService]属性修饰了服务类?还可以尝试将数据参数更改为:data: { }FireBug对此有何评论?是否有请求被发送?如果是,服务器会做出什么反应?

您的网址也有错误(网络服务具有ASMX扩展名)。你写道:

CustomerImport.aspx/ExecuteImport

虽然它应该是:

CustomerImport.asmx/ExecuteImport

这是一个完整的工作示例,您可以根据自己的需要进行调整:

网络服务:

[WebService(Namespace = "http://tempuri.org/")]
[WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]
[ToolboxItem(false)]
[ScriptService]
public class CustomerImport : WebService
{
    [WebMethod]
    public void ExecuteImport()
    {
    }
}

致电网页:

<%@ Page Language="C#" %>
<!doctype html>
<html>
<head>
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
    <title>Test</title>
    <script type="text/javascript" src="scripts/jquery-1.4.1.js"></script>
    <script type="text/javascript">
        $(function () {
            $.ajax({
                type: 'POST',
                url: '/CustomerImport.asmx/ExecuteImport',
                data: { },
                success: function () {
                    alert('ok');
                }
            });
        });
    </script>
</head>
<body>

    <form runat="server">

    </form>

</body>
</html>

答案 1 :(得分:1)

将错误功能添加到ajax调用中...希望您能获得一些信息,以了解调用失败的原因。

你正在使用firebug吗?观看网络标签。

 $.ajax({
    type: "POST",
    url: url,
    async: false,
    data: jsonEncodedParams,
    contentType: "application/json; charset=utf-8",
    dataType: "json",
    success: function (msg) {

    }, //success
    error: function (XMLHttpRequest, textStatus, errorThrown) {
      if (textStatus == "timeout") {
        alert('The request timed out, please resubmit');
      } //if
      else {
        alert(errorThrown);
      } //else
    } //error
  }); //ajax

答案 2 :(得分:1)

由于您的服务器端端点是“页面方法”,it must be declared as static

[System.Web.Services.WebMethod]
public static void ExecuteImport()
{
    _Presenter.ExecuteImport();
}