While working on a webform project in Visual Studio, I encountered an issue when trying to send data from the form fields to a 3rd party API using a POST request. Despite my attempts to use JSON to capture the form field data and send it as a JSON object, I kept encountering an error message stating "Authorization has been denied for this request." This problem became quite frustrating for me since I am relatively new to ASP and dealing with JSON.
Below is a snippet of sample code that resembles what I was working with (please note that this is not the actual code):
<form action='https://test' method="post" name="MY Form">
<input id="username" name="username" type="text" /><br />
<input id="password" name="password" type="password" /><br />
<input id="button1" name="button1" type="submit" value="login" />
</form>
JavaScript:
$('#button1').click(function() {
formData = {
username: username,
password: password
};
$.ajax({
type: 'POST',
contentType: 'application/json',
accept: 'application/json',
authorization: 'Bearer <token>',
url: 'https://test',
dataType: 'json',
data: formData
});
});