angular.js - angularjs http settings headers
習慣沉默
習慣沉默 2017-05-15 16:57:52
0
4
834

I want to use http in angularjs to send a request to the backend. The backend is based on tornado. Now there is a token uniquely recognized by the user that I want to put in the headers, which is {headres:{'token':1}}, But I have tried many methods but it doesn’t work. I would like to ask what should be done. Do I need to set up something in the background? Thank you

習慣沉默
習慣沉默

reply all(4)
我想大声告诉你

Routing configuration$httpProvider.defaults.headers.post['token'] = '123';

Peter_Zhu

Place the following interceptors in config

$httpProvider.interceptors.push(['$rootScope', '$q', '$localStorage', function ($rootScope, $q, $localStorage) {
      return {
        request: function (config) {

          // Header - Token
          config.headers = config.headers || {};
          if ($localStorage.token) {
            config.headers.token = $localStorage.token;
          };
          
          return config;
        },

        response: function (response) {

          if (response.status == 200) {
            // console.log('do something...');
          }
          
          return response || $q.when(response);
        },

        responseError: function (response) {
        
          return $q.reject(response);
        }
      }
    }])
Peter_Zhu
 $.ajax({
                    type: "GET",
                    url: "xxxx",
                    beforeSend: function(request) {
                        request.setRequestHeader("Token", "1");
                    },
                    success: function(result) {
                        alert(result);
                    }
                });

But why use headers, cookies, and request parameters?

淡淡烟草味

Has it been solved? I have this problem

Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template