How can I set a custom field in POST header on submit a form?
相关问题
- Views base64 encoded blob in HTML with PHP
- Is there a limit to how many levels you can nest i
- How to toggle on Order in ReactJS
- void before promise syntax
- Keeping track of variable instances
From FormData documention:
With an
XMLHttpRequest
you can set the custom headers and then do thePOST
.To add into every ajax request, I have answered it here: https://stackoverflow.com/a/58964440/1909708
To add into particular ajax requests, this' how I implemented:
You must add the
meta
elements in the JSP, e.g.To add to a form submission (synchronous) request, I have answered it here: https://stackoverflow.com/a/58965526/1909708
Set a cookie value on the page, and then read it back server side.
You won't be able to set a specific header, but the value will be accessible in the headers section and not the content body.
You could use $.ajax to avoid the natural behaviour of
<form method="POST">
. You could, for example, add an event to the submission button and treat the POST request as AJAX.Here is what I did in pub/jade
In fact a better way to do it to save a cookie on the client side. Then the cookie is automatically sent with every page header for that particular domain.
In node-js, you can set up and use cookies with cookie-parser.
an example:
Now you can access this :
Note that
httpOnly:true
ensures that the cookie is usually not accessible manually or through javascript and only browser can access it. If you want to send some headers or security tokens with a form post, and not through ajax, in most situation this can be considered a secure way. Although make sure that the data is sent over secure protocol /ssl if you are storing some sensitive user related info which is usually the case.