ASP.NET problem with jQuery ajax function

I have a button and 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",
                    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;
                    }
                }
            });
        }

method:

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

the problem is that this method is NOT called. Why?

When I replace $.ajaxwith eg alert('ok');, a warning appears, so it works

+3
source share
3 answers

Have you decorated your service class with an attribute [ScriptService]? Also, try changing the parameter data: data: { }. What does FireBug say about this? Is there a sending request? If so, what will the server respond?

You also have an error in your URL (web services have the .asmx extension). You wrote:

CustomerImport.aspx/ExecuteImport

For now, this should be:

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>
+3

ajax... , , .

firebug? net.

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

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

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

Source: https://habr.com/ru/post/1756893/


All Articles