Reputation: 399
Is it possible to parse/add an authentication on the submit from a standard HTML for? E.g. I'm using oAuth to authentication logged-in users, and have a usecase where I need to use a standard HTML form with the action and method attributes. But I can't seem to find a way to parse the JWT Token I'm using for authentication. Is this possible?
Upvotes: 2
Views: 3180
Reputation: 12322
No, you can't do it with a regular HTML form. If you need to add an Authorization
header to the request, the only thing you can do is to make a request from the Javascript (as is shown in Subhashis's answer). It doesn't have to be jQuery though, you can use plain JS and fetch or some libraries for making http calls (e.g. axios). Whichever you use, remember that it will be an AJAX call, so your JS will have to handle the response properly (the response will not be handled by the browser automatically).
Upvotes: 3
Reputation: 1538
You may use JQuery form submit to send authorization header
var form = $('#form-id').get(0);
var formData = new FormData(form);
var xhr = new XMLHttpRequest();
xhr.open("POST", "url");
xhr.setRequestHeader("Authorization", "jwt token");
xhr.send(formData);
Upvotes: 1