1

我正在使用 angularjs - 1.6。

我需要明确地向服务器发送一个 http GET 请求以及一个 cookie。

我的意思是,我知道一旦我们成功完成授权,每个 http 请求都会自动发送 cookie。

但就我而言,我需要明确发送。我怎样才能做到这一点。下面是https代码:

$cookies.put('JSESSIONID', 'lu5xxkdqgjk5qpv07ufhvln3');
 $http({
         url:"http://10.11.0.11:4440/api/21/projects",
          method:"GET",
          headers: { 'Accept': 'application/json',
                     'X-Rundeck-Auth-Token':'lu5xxkdqgjk5qpv07ufhvln3'},
         "Access-Control-Allow-Credentials" : true
      }).then(function(response) {
          $scope.response = response.data;
          alert("Report fot succeed"+response.data);
      }, function(response) {
          alert("error response:"+response.data);
      }); 
4

1 回答 1

1

您可以通过多种方式做到这一点。

如果您坚持认为它应该是 GET,则可以将 cookie 的值作为标头之一发送:

headers: {'Cookie': 'Value', ...}

当然,您可以事先获取 cookie 的值,然后将其分配给某个变量,然后将'Cookie'header 设置为该值。

headers: {'Cookie': myVariable, ...}

记得设置

 $httpProvider.defaults.withCredentials = true;

.config档案中。

如果要发送的数据更复杂,您应该考虑将 GET 更改为 POST,因为提到 POST 来发送数据,例如:

   this.login = function (loginInfo) {

        return $http({
            url: 'your url',
            headers: {
                'Content-Type' : 'your content type'
                // other headers..
            },
            method: 'POST',
            data: {
                user: {
                    name: loginInfo.nick,
                    password: loginInfo.password
                }
            }
        })
        .then(function(response) {
             console.log('success');
        });
   }
于 2018-03-19T07:24:13.850 回答