Using jQuery and JSON to use Asp.net server-side asynchronous web methods

I defined two calls to webservices in ASP.NET: 1.BeginLengthyProcedure and EndLengthyProcedure (asynchronous web method) 2.LengthProcedureSync

namespace ServiceDemo
{
[WebService(Namespace = "http://tempuri.org/")]
[WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]
[System.ComponentModel.ToolboxItem(false)]
[System.Web.Script.Services.ScriptService]
public class AsyncWebService : System.Web.Services.WebService
{
    public delegate string LengthyProcedureAsyncStub(int milliseconds);

    public string LengthyProcedure(int milliseconds)
    {
        System.Threading.Thread.Sleep(milliseconds);
        return "SuccessAsync";
    }

    public class MyState
    {
        public object previousState;
        public LengthyProcedureAsyncStub asyncStub;
    }

    [WebMethod]
    [ScriptMethod(ResponseFormat = ResponseFormat.Json)]
    public IAsyncResult BeginLengthyProcedure(int milliseconds, AsyncCallback cb, object s)
    {
        LengthyProcedureAsyncStub stub = new LengthyProcedureAsyncStub(LengthyProcedure);
        MyState ms = new MyState();
        ms.previousState = s;
        ms.asyncStub = stub;
        return stub.BeginInvoke(milliseconds, cb, ms);
    }

    [WebMethod]
    [ScriptMethod(ResponseFormat = ResponseFormat.Json)]
    public string EndLengthyProcedure(IAsyncResult call)
    {
        MyState ms = (MyState)call.AsyncState;
        string res = ms.asyncStub.EndInvoke(call);
        return res;
    }

    [WebMethod]
    [ScriptMethod(ResponseFormat = ResponseFormat.Json)]
    public string LengthyProcedureSync(int milliseconds)
    {
        System.Threading.Thread.Sleep(milliseconds);
        return "SuccessSync";
    }
}
}

Then I used them using jQuery, I do not want to use ASP.NET AJAX. LengthProcedureSync worked fine, but LenghtyProcudure did not work. Did I miss something?

<script type="text/javascript" src="jquery-1.4.4.min.js"></script>

<script type="text/javascript">
    function testJson() {
        $.ajax({
            type: "POST",
            //Didn't work
            url: "AsyncWebService.asmx/LengthyProcedure",
            //Work
            //url: "AsyncWebService.asmx/LengthyProcedureSync",
            data: "{'milliseconds':'2000'}",
            contentType: "application/json; charset=utf-8",
            dataType: "json",
            success: function (msg) {
                $("#jsonResponse").html(msg.d);
            },
            error: function (msg) {

            }

        });
    };

</script>
+3
source share
2 answers

, - ASP.NET AJAX.NET Framework ( JSON) -, - ASP.NET . " Web 2.0 ASP.NET 3.5", Omar AL Zabir, - ASP.NET AJAX (. 7, . 177). , MSDN. , !

+1

public string LengthyProcedure(int milliseconds)
     {
         System.Threading.Thread.Sleep(milliseconds);
         return "SuccessAsync";
     } 

:

[WebMethod]
[ScriptMethod(ResponseFormat = ResponseFormat.Json)]
public string LengthyProcedure(int milliseconds)
     {
           System.Threading.Thread.Sleep(milliseconds);         
           return "SuccessAsync";
     } 
0

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


All Articles