将标题添加到node.js

时间:2015-08-06 18:39:03

标签: javascript jquery node.js

我无法在以下POST请求中设置任何标头,以便将照片上传到node.js.标头存在但缺少x-access-token。当请求进入时,我正在将请求标头记录到控制台,并且没有'x-access-token',并且我的用户在此步骤中验证失败,我的node.js中间件授权受保护路由的令牌。

我已经通过将令牌附加到URL来尝试使用req.param,但这不起作用并在浏览器中抛出CAN NOT POST / photo错误。

var auth = localStorage.getItem('token');
$.ajax({
    url: '/photo',
    headers: {'x-access-token': auth},
    data: new FormData( this ),
    cache: false,
    contentType: false,
    processData: false,
    type: "POST",
    success:function(result, success) {
        console.log(JSON.stringify(result));
    },
    error:function(result, success) {
        console.log(JSON.stringify(result));
    }
});

记录的请求标头:

14:49:54 web.1  | request headers { host: 'localhost:5000',
14:49:54 web.1  |   connection: 'keep-alive',
14:49:54 web.1  |   'content-length': '38477',
14:49:54 web.1  |   'cache-control': 'max-age=0',
14:49:54 web.1  |   accept: 'text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,*/*;q=0.8',
14:49:54 web.1  |   origin: 'http://localhost:5000',
14:49:54 web.1  |   'upgrade-insecure-requests': '1',
14:49:54 web.1  |   'user-agent': 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_10_0) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/44.0.2403.125 Safari/537.36',
14:49:54 web.1  |   'content-type': 'multipart/form-data; boundary=----WebKitFormBoundaryA9UW3T02fcX803jG',
14:49:54 web.1  |   dnt: '1',
14:49:54 web.1  |   referer: 'http://localhost:5000/profile/eyJ0eXAiOiJKV1QiLCJhbGciOiJIUzI1NiJ9.eyJ1c2VyIjozLCJ1c2VybmFtZSI6Imhhc2hlZG1hbiIsImlhdCI6MTQzODg4Njk4NywiZXhwIjoxNDM4OTczMzg3fQ.upLUzkW2g2nQEdga6_hvvyzgTD0H2ciwYLA6zR0T_C4',
14:49:54 web.1  |   'accept-encoding': 'gzip, deflate',
14:49:54 web.1  |   'accept-language': 'en-US,en;q=0.8,fr;q=0.6' }

0 个答案:

没有答案