如何使用$ resource在标题中携带令牌?

时间:2017-05-13 09:33:43

标签: angularjs jwt ngresource

我成功使用此命令获取终端中的数据。

curl -H "Authorization: JWT some-token-code" localhost:8001/api/users/

但是在angular1.x app.factory.js中。它返回GET http://localhost:8001/api/users/ 401 (Unauthorized)

function(token) {
                console.log('JWT ' + token)
                return $resource("/api/users/ ", {}, {
                    headers: {
                        'Authorization': 'JWT ' + token
                    }
                })
            }

这是请求标头 GET /api/users/ HTTP/1.1 Host: localhost:8001 Connection: keep-alive Accept: application/json, text/plain, */* User-Agent: Mozilla/5.0 (Macintosh; Intel Mac OS X 10_12_4) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/58.0.3029.110 Safari/537.36 Referer: http://localhost:8001/ Accept-Encoding: gzip, deflate, sdch, br Accept-Language: zh-CN,zh;q=0.8 Cookie: JESONG_USER_ID=01000000003981226574343581913557; _ga=GA1.1.630936919.1492924579; __utma=1.630936919.1492924579.1492924580.1492924580.1; __utmz=1.1492924580.1.1.utmcsr=(direct)|utmccn=(direct)|utmcmd=(none); Hm_lvt_a5aaaf14eb4ea4f77e46c672b6e33b31=1492924579; Hm_lvt_e010d1faf316a4dbfe8639481a2a3f90=1492930657; sessionid=ic2svmb4wyec67gz1opv5xbpl8fvc1vr; csrftoken=pQWQTS0xhk7hY5d1m3OABxbDPIFbkLSI3kDC0bcIwCDzm0wEmjsu6PbFsalfMgnz; token=eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJ1c2VybmFtZSI6ImFkbWluIiwidXNlcl9pZCI6MSwiZW1haWwiOiJhZG1pbkBhLmNjIiwiZXhwIjoxNDk0NjY3NDI5fQ.Zs2fwu97QvyWqL1dzuf6VSfLLDOsbs-M6KeIy2cSpIg; username=admin; name=; identity=%E7%AE%A1%E7%90%86%E5%91%98

我不知道问题出在哪里。

如何使用$ resource在命令行中模拟HTTP请求(如何配置标头)?

初学者,谢谢你的教学。

0 个答案:

没有答案