0

I am using the new mobile tool Apache codova with jquery mobile in visual studio. I am trying to make ajax request to asp.net web api to login. using /Token in the default individual account. The request returns error in the ajax but when i check fiddle the token is return with status of 200 OK, i want the ajax success function to return the token. Any help will be appreciated.

This is my code:

var loginData = {
        grant_type: "password",
        username: $("#LoginEmail").val(),
        password: $("#LoginPassword").val()
    };

    $.ajax({
        type: 'POST',
        url: storeUrl + '/Token',
        data: loginData
    }).done(function (data) {
        alert(data.userName);
        alert(data.access_token);

    }).fail(showError);

    function showError(req, s, t)
    {
        alert('Request Status: ' + req.status + ' Status Text: ' + req.statusText + ' ' + req.responseText);
    }
Johnson Duru
  • 662
  • 2
  • 13
  • 27

2 Answers2

2

I fix this by adding .

context.OwinContext.Response.Headers.Add("Access-Control-Allow-Origin", new[] { "*" });

In the GrantResourceOwnerCredentials() function of ApplicationOAuthProvider class of my web api project.

Johnson Duru
  • 662
  • 2
  • 13
  • 27
0

add properties to ajax call:

contentType: 'application/json; charset=utf-8',
dataType: 'json'

refer to this question

Community
  • 1
  • 1
aleha_84
  • 8,309
  • 2
  • 38
  • 46