locked
jquery ajax return value RRS feed

  • Question

  • User-674900201 posted

    I have the following code:

    $.ajax({
        type: "POST",
        contentType: "application/json; charset=utf-8",
        url: 'ajax.aspx/CheckName',
        data: "{'Name':'TestName'}",
        dataType: "json",
        success: function (data) {
            alert(data.d);     // first alert with empty message
            alert(data.d);     // second alert with return value
        },
        error: function (data) {
            alert("error");
        }
    });
    <WebMethod()>
    Public Shared Function checkName(Name As String) As String
        Return Name
    End Function

    The first alert() gives me an empty MessageBox. The second alert() the correct return value. why?

    Thanks for help!

    Wednesday, January 13, 2021 7:55 AM

All replies

  • User475983607 posted

    I cannot reproduce this issue.

    Wednesday, January 13, 2021 12:34 PM
  • User-1330468790 posted

    Hi trashman2001,

     

    I use exactly the same codes as you provided and it returns me with two alerts and values successfully.

     

    Could you please confirm that you have used these codes and got the unexpected result?

    If you have already simplified the codes for us, you could re-post the whole codes which you used so that we could reproduce the problem?

     

    Best regards,

    Sean

     

    Thursday, January 14, 2021 6:07 AM
  • User-674900201 posted

    Hi Sean,

    you're right. I have simplified the code.

    I looked further myself. Can it be, that client side javascript continues although the server side result is not back?

    (i hope you understand me)

    Friday, January 15, 2021 8:39 AM
  • User-1330468790 posted

    Hi trashman2001,

      

    trashman2001

    Can it be, that client side javascript continues although the server side result is not back?

    No, it cannot be what you illustrated.

    The alert function will and only be triggered when ajax gets a response from server side. As you can see, the call back function name is "success" which means the server successfully gets the request and returns the ajax function with '200' http status code.

      

    You could refer to this documentation for .ajax() and its property 'success'.

    https://api.jquery.com/jquery.ajax/

    <div>success</div> <div>Type: FunctionAnything data, String textStatus, jqXHR jqXHR )</div> <div>A function to be called if the request succeeds. The function gets passed three arguments: The data returned from the server, formatted according to the dataType parameter or the dataFilter callback function, if specified; a string describing the status; and the jqXHR (in jQuery 1.4.x, XMLHttpRequest) object. As of jQuery 1.5, the success setting can accept an array of functions. Each function will be called in turn. This is an Ajax Event.</div>

    Best regards,

    Sean

    Friday, January 15, 2021 11:00 AM
  • User475983607 posted

    you're right. I have simplified the code.

    I looked further myself. Can it be, that client side javascript continues although the server side result is not back?

    If I understand your response, the code you shared above is not the actual code that has a problem.  JavaScript is asynchronous so it is possible you have a bug in the logic.  The community needs the actual code if you are looking for a community code review.

    Friday, January 15, 2021 11:07 AM