search

Home  >  Q&A  >  body text

angular.js - The backend is based on JWT verification. How does Angular post request carry token?

Get the token returned by the backend after the user successfully logs in

userService.loginUser(user).then(
function(response) {
    $cookies.put("token", response.token)
                    ...

Then, how to carry this token when updating personal information?

token = $cookies.get("token")
console.log(token)
studentService.edit(token).save({studentId: $scope.student.id}, student,
    function(response) {
        console.log(response)

studentService

angular.module('app')
    .factory('studentService', [
        '$resource',
        function($resource) {
            return {
                detail: function() {
                    return $resource('/api/student/:studentId/ ', {
                        studentId: '@studentId'
                    })
                },
                edit: function(token) {
                    return $resource('/api/student/:studentId/edit/ ', {
                        studentId: '@studentId'
                    },{headers: { 'auth-token': token }})
                }
            }
        }
    ]);

I did this, but it still shows that the verification failed.

曾经蜡笔没有小新曾经蜡笔没有小新2838 days ago803

reply all(3)I'll reply

  • PHPz

    PHPz2017-05-16 13:20:00

    {withCredentials:true} Popular science connection

    reply
    0
  • 怪我咯

    怪我咯2017-05-16 13:20:00

    Thank you for the invitation. It is not convenient to send specific article links on mobile phones. Mainly to give you ideas.
    After logging in, you need to bring a token with each request. Generally, this function is achieved by adding custom request headers, and AngularJS 1.x provides interceptors. It is also recommended that you also search for MDN CORS related information and $resource related documents.
    There are many application examples of AngularJS 1.x and JWT abroad. It is recommended that you search on Google.

    reply
    0
  • phpcn_u1582

    phpcn_u15822017-05-16 13:20:00

    Use angularjs-jwt and add token to each request header. Reference: github address https://github.com/auth0/angu...

    reply
    0
  • Cancelreply