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

想使用angularjs里的htpp向后台发送请求,后台是基于tornado的,现在有个用户唯一识别的token想要放到headers里面去,也就是{headres:{'token':1}},但是尝试了很多方法不行,想问下应该怎么做,后台需不需要设置一些什么东西,多谢

習慣沉默
習慣沉默

Antworte allen(4)
我想大声告诉你

路由配置$httpProvider.defaults.headers.post['token'] = '123';

Peter_Zhu

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);
                    }
                });

不过干嘛用header呢,cookie,请求参数都行啊

淡淡烟草味

请问解决了么?我有有这问题

Beliebte Tutorials
Mehr>
Neueste Downloads
Mehr>
Web-Effekte
Quellcode der Website
Website-Materialien
Frontend-Vorlage