How to add Custom headers in ajax call
when i send ajax based call to api without header then it's working fine but i add some custom header like Authorization then it's does not add the header and i am getting error...
any one is there who's help....
- $("#Loginform").submit(function () {
- var token = localStorage.getItem("AccessToken");
- var username = $("#username").val();
- var password = $("#password").val();
- $.ajax({
- url: $(this).attr('action'),
- data: {
- UserName: username,
- Password: password
- },
- method: 'POST',
- contentType: "application/x-www-form-urlencoded; charset=urf-8",
- dataType: 'json',
- beforeSend: function (xhr) {
- alert(token);
- xhr.setRequestHeader('Authorization', "Bearer" + " " + token);
- }
- }).done(function (data) {
- if (data.status == 'success')
- {
- window.location.href = "/Home/Index";
- }
- if (data.status = "error")
- {
- alert("ErRoR");
- }
- }).error(function (jqXHR, status, errorThrown) {
- alert(status+"ok");
- });
- });