0

I'm new to the .Net development and i want to make a call to the client server. When i'm running the lines of code with POSTMAN, it runs pretty well. But when i'm using the same code/headers in java script i'm able to get the desired result.

Below is the line of code i'm using

 var xhr = new XMLHttpRequest();
 var params = "grant_type=password";
 xhr.open("POST", "ClientURL");
 xhr.setRequestHeader("Authorization", "Basic ZDJWQ1NmE4NjA4MDc0MjQ2NzSfdsdakj=​");
 xhr.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");
 xhr.setRequestHeader("Data-Type", "json");
 xhr.setRequestHeader("Host", "URL");

 xhr.send(params);
 console.log(xhr.status);
 console.log(xhr.statusText)

Or

$.ajax({
    type: "POST",
    url: 'CLIENT URL',
    dataType: "json",
    headers: { 'Authorization': 'Basic ZDJWQ1NmE4NjA4MDc0MjQ2NzSfdsdakj=​' },
    beforeSend: function (xhr) { 
        xhr.setRequestHeader('Authorization', 'Basic "ZDJWQ1NmE4NjA4MDc0MjQ2NzSfdsdakj=​"​'); 
    },
    success: function (data) {
        if (data) {
            alert(data);
        }
        else {
            alert("Something went wrong");
        }
    },
    error: function (xhr, ajaxOptions, thrownError) {
        alert(thrownError);
    }
});

Is there anything missing in the call? Any help is appreciated

Below it the error in IE

XMLHttpRequest: Network Error 0x80070005, Access is denied.

and in Chrome

Default.aspx:68 Uncaught SyntaxError: Failed to execute 'setRequestHeader' on 'XMLHttpRequest': 'Basic ZDJWQ1NmE4NjA4MDc0MjQ2NzSfdsdakj=​' is not a valid HTTP header field value.
gurmandeep
  • 1,227
  • 1
  • 14
  • 30

1 Answers1

0
beforeSend: function (xhr) { 
    xhr.setRequestHeader('Authorization', "Basic ZDJWQ1NmE4NjA4MDc0MjQ2NzSfdsdakj=​"​); 
},

change the function as above.

vijay
  • 846
  • 9
  • 11