rakshi rakshi - 10 months ago 54
jQuery Question

Authorization header sent from jquery to web API client is not properly received

I am doing a web api authorization

I making an authorization call from jquery to the web API with following code

<script src="Scripts/jquery-1.10.2.min.js"></script>
<script>
$(document).ready(function(){
var ulEmployees = $('#ulEmployee');
var username = $('#uname').val();
var password = $('#pword').val();
username = "male";
password = "male";
$('#btn').click(function () {

$.ajax({

type: 'GET',
url: 'api/Employees',
dataType: 'json',
headers: {

'Authorization': 'Basic' + btoa(username + ':' + password)

},

success: function (data)
{
ulEmployees.empty();
$.each(data, function (index, val) {

var fullName = val.FirstName + ''+val.gender + '' + val.LastName;
ulEmployees.append('<li>' + fullName + '</li>');

});
},


});
});



$('#btnclear').click(function(){

ulEmployees.empty();

});


});

});

</script>


but at controller side, I am not properly receiving the data

as bellow

actionContext.Request.Headers.Authorization.Parameter=null

actionContext.Request.Headers.Authorization.Scheme="BasicbWFsZTptYWxl"

but accually i need as bellow

actionContext.Request.Headers.Authorization.Parameter="bWFsZTptYWxl"

actionContext.Request.Headers.Authorization.Scheme="Basic"

Answer Source

Looks like you need append one space symbol after 'Basic' in

'Authorization': 'Basic' + btoa(username + ':' + password)

So this string might be looks like

'Authorization': 'Basic ' + btoa(username + ':' + password)
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download