vendredi 31 juillet 2015

JQuery.ajax does not wait for C# Server's response and calls error function, testing on localhost

On the user-side, I have a javascript code, that POSTs to the server via JQuery.ajax. On the server-side, I receive the request and handle it, but the user-side script does not wait for the servers response and calls error: getFail (see below) function.

How did I check if the user-side waits for the server or not?
I've just run the server in debug mode, and set a breakpoint where it receives the POST-ed values. And the user-side already called the error function.

  • Could anybody tell me, why it is not working "properly" or what and where is the source of error?

What I suspect to be one of the sources of the "improper" behavior is the dataType parameter of the JQuery.ajax function. It is supposed to specify the expected type of the return value from the server. I am not sure what to set this, so I did not specify hoping the default will find out.

[For the dataType parameter] If none is specified, jQuery will try to infer it based on the MIME type of the response. Possible types are: xml, html, script, json, jsonp, text, multiple. (Source: http://ift.tt/gmB8Sv, JQuery docs).


Here are the codes:

User-side javascript code:

function logIn() {
try {
    alert('try in logIn');
    jQuery.ajax({
        type: "POST",
        url: "http://localhost:8080/Token",
        cache: false,
        data: {
            "grant_type": "password",
            "username": document.getElementById("username").value,
            "password": document.getElementById("password").value
        },
        contentType: "application/x-www-form-urlencoded", // data type sent to server
        // dataType: "json", // data type expected from server <- THIS may be a source of the problem
        success: getSuccess,
        error: getFail
    });
} catch (e) {
    alert(e);
}
function getSuccess(data, textStatus, jqXHR) {
    alert(data.Response);
};
function getFail(jqXHR, textStatus, errorThrown) {
    //jqXHR.status is 0
    //textStatus is "error"
    //errorThrown is empty
    alert(jqXHR.status); // <-- THIS is what gets called, instantly after the post.
};

};

Server-side C# code:

public class ApplicationOAuthServerProvider
    : OAuthAuthorizationServerProvider
{
    public override async Task ValidateClientAuthentication(
        OAuthValidateClientAuthenticationContext context)
    {
        await Task.FromResult(context.Validated()); // break-point was here
    }

    // This is called by await Task.FromResult(context.Validated())
    public override async Task GrantResourceOwnerCredentials(
        OAuthGrantResourceOwnerCredentialsContext context)
    {
        // Here manager will have correct values from the POST-ed data
        var manager = context.OwinContext.GetUserManager<ApplicationUserManager>();

        var user = await manager.FindAsync(context.UserName, context.Password);
        if (user == null)
        {
            context.SetError(
                "invalid_grant", "The user name or password is incorrect.");
            context.Rejected();
            return;
        }

        foreach (var userClaim in user.Claims)
        {
            identity.AddClaim(new Claim(userClaim.ClaimType, userClaim.ClaimValue));
        }

        context.Validated(identity);
    }
}

Aucun commentaire:

Enregistrer un commentaire