search

Home  >  Q&A  >  body text

angular.js - angularjs http settings headers

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

習慣沉默習慣沉默2867 days ago851

reply all(4)I'll reply

  • 我想大声告诉你

    我想大声告诉你2017-05-15 16:59:52

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

    reply
    0
  • 天蓬老师

    天蓬老师2017-05-15 16:59:52

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

    reply
    0
  • 天蓬老师

    天蓬老师2017-05-15 16:59:52

     $.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?

    reply
    0
  • 淡淡烟草味

    淡淡烟草味2017-05-15 16:59:52

    Has it been solved? I have this problem

    reply
    0
  • Cancelreply