How to set a Header field on POST a form?

前端 未结 8 1906
情书的邮戳
情书的邮戳 2020-11-27 04:08

How can I set a custom field in POST header on submit a form?

8条回答
  •  悲哀的现实
    2020-11-27 04:49

    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:

    res.cookie('token', "xyz....", { httpOnly: true });
    

    Now you can access this :

    app.get('/',function(req, res){
     var token = req.cookies.token
    });
    

    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.

提交回复
热议问题