【问题标题】:How to pass cookie as header in angularjs to a remote application that uses drupal [403 Forbidden]如何将 cookie 作为 angularjs 中的标头传递给使用 drupal [403 Forbidden] 的远程应用程序
【发布时间】:2017-03-15 15:20:29
【问题描述】:

美好的一天,我正在尝试向在 agular.js 中使用 drupal 的远程应用程序发出 $http 获取请求。每次我发出获取请求时,我都会收到一个 403 响应,上面写着 [匿名用户]。我能够发出 put 请求并获得带有令牌、session_name 和 session_id 的响应。我的问题是 我不确定如何将 cookie 作为请求标头的一部分传递。 ** **我尝试将 cookie 作为标头传递,但仍然收到 403 响应。 任何建议都会有所帮助。我什至尝试使用 $cookie.put('KEY,cookie) 来保存 cookie,但仍然得到相同的响应。

var headers = {'Content-Type':'application/json','Cookie':xxxxxx=tokexxxx};

$http.get(url,headers).success(function(data){
        // response of the results 
        console.log('this is the repsonse that come from the get request', response);
}).error(function(err){// 403 error});

我的登录服务如下所示

angular.module('app.services', []).service('LoginService', function($rootScope,$q,$http,$cookies) {

    // this is the function that is called when ther user logins 
       var service = {};
       // get cridentials of the user 
       service.LogIn = function(username,password,callback){
            var loginStatus = [];
            var BASEURL=  http://xxxxxx.com';
            var parameters ={username: username,password: password};
            var headers = {'Content-Type':'application/json'}

            $http.post(BASEURL,parameters,headers).success(function (response) {
              //  cookie.put
              // console.log('response::', {sessionid,token,session,user_data}
             $cookies.put(response.session_name,response.token); //
              console.log(response.session_name,response.token);
              localStorage.setItem('sessid',response.sessid); // locastorage 
              localStorage.setItem('token',response.token);
              console.log(response);
               return callback(response); //call this  on the controller
            });
       }

【问题讨论】:

    标签: javascript angularjs rest cookies drupal-7


    【解决方案1】:

    也许可以尝试代替“cookie:”输入“xsrfCookieName”并提供 cookie 名称。 它应该使用其中的令牌。

    var headers = {'Content-Type':'application/json','xsrfCookieName':'cookieName'};
    

    希望对你有帮助

    【讨论】:

    • cookieName 不应该是 Cookie 的值吗?
    • 嗯,cookie 更像是一本字典。 key, value 当你在 headers 中做 $cookies.put(name, value) 时,当你提供 cookie 名称时,它会放置 cookie 值。
    • 谢谢,我只是需要澄清一下
    猜你喜欢
    • 2023-03-15
    • 1970-01-01
    • 2013-04-21
    • 1970-01-01
    • 2014-09-18
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-04-21
    相关资源
    最近更新 更多