开发者

ASP.NET a problem with jQuery's ajax function

I have button and that jQuery script (to start the progress bar):

<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",
                    dat开发者_开发百科aType: "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;
                    }
                }
            });
        }

the method:

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

the problem is, that method is NOT being invoked. Why ?

When I replace the $.ajax for the e.g alert('ok'); the alert shows, so it works


Did you decorate your service class with the [ScriptService] attribute? Also try changing the data parameter to: data: { }. What does FireBug says about this? Is there a request being sent? If yes what does the server respond?

Also you have a mistake in your url (web services have the ASMX extension). You wrote:

CustomerImport.aspx/ExecuteImport

While it should be:

CustomerImport.asmx/ExecuteImport

Here's a complete working example that you may adapt to your needs:

Web service:

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

Calling web page:

<%@ 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>


Add in the error function to the ajax call... Hopefully you'll get some info back, as to why the call failed.

Are you using firebug? Watch the net tab.

 $.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


Since your server-side endpoint is a "page method", it must be declared as static:

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

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜