简体   繁体   English

使用express.js在响应头中设置标记值

[英]Set token value in response header using express.js

I am having trouble setting token into header. 我无法将令牌设置为标头。 I read express.js 4 doc at 我读了express.js 4 doc at

http://expressjs.com/4x/api.html#res.set http://expressjs.com/4x/api.html#res.set

and it stated like 它说的像

res.set('token', 'kjhdkf89q37453lajjfq23');

Below are how I configure express.js 以下是我配置express.js的方法

var restful_express = express(); restful_express.use(bodyParser());

var allowCrossDomain = function(req, res, next) {
    res.header('Access-Control-Allow-Origin', '*');
    res.header('Access-Control-Allow-Methods', 'GET,POST');
    next();
}

restful_express.use(allowCrossDomain);

restful_express.listen(7788, function() {

});

How I sent response back 我是如何回复的

res.set('token', 'kjhdkf89q37453lajjfq23');

res.json({userid:"123123678"});

and when I retrieve using jQuery and the value I got are below: 当我使用jQuery检索时,我得到的值如下:

$.post( $('#url').val() + "/login", {email:"abab@gmail.com"}).done(function( data, textStatus, request ) {

    console.log(data.userid);
    // 123123678

    console.log(textStatus);
    // success

    console.log(request.getAllResponseHeaders()); 
    // Content-Type: application/json 

    console.log(request.getResponseHeader("token")); 
    // null

});

Appreciate any advice please. 请欣赏任何建议。 Thanks ! 谢谢 !

regards, Mark 问候,马克

对于CORS,您还需要设置Access-Control-Expose-Headers以使浏览器知道允许访问哪些自定义标头。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM