How to get Access Token from ASP.Net Web API 2 via AngularJS $http?

前端 未结 6 1918
孤街浪徒
孤街浪徒 2021-02-04 02:12

I try like this:

$http({ method: \'POST\', url: \'/token\', data: { username: $scope.username, password: $scope.password, grant_type: \'password\' } }).success(f         


        
6条回答
  •  旧巷少年郎
    2021-02-04 02:48

    1) Note the URL: "localhost:55828/token" (not "localhost:55828/API/token")

    2) Note the request data. Its not in json format, its just plain data without double quotes. "userName=xxx@gmail.com&password=Test123$&grant_type=password"

    3) Note the content type. Content-Type: 'application/x-www-form-urlencoded' (not Content-Type: 'application/json')

    4) When you use javascript to make post request, you may use following:

    $http.post("localhost:55828/token", 
        "userName=" + encodeURIComponent(email) +
            "&password=" + encodeURIComponent(password) +
            "&grant_type=password",
        {headers: { 'Content-Type': 'application/x-www-form-urlencoded' }}
    ).success(function (data) {//...
    

    See screenshots below from Postman:

    Postman Request

    Postman Request Header

提交回复
热议问题