读/写来自XHR请求/响应的Cookie(PhoneGap / Cordova) [英] Read/Write the cookies from an XHR request/response (PhoneGap/Cordova)

查看:216
本文介绍了读/写来自XHR请求/响应的Cookie(PhoneGap / Cordova)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用$ http(angular)并发布到一个远程的网站,我不能控制。

当登录时,它给我一个'site_session'我想要读的cookie。
请注意,由于在Cordova应用程序中在移动设备上运行,因此我可以呼叫远程网站(跨网域)。

I am using $http (angular) and posting to a distant website which I don't control.
When logging in, it gives me a 'site_session' cookie which I want to read. Please note that I am able to call a remote website (cross-domain) because running on a mobile device, in a Cordova application.

我试过全部

我使用Angular 1.3.13,因此在之后的 $ cookies $ timeout 应该工作,但不是。

我尝试了 withCredentials = true $ httpProvider.defaults和$ http呼叫的配置本身:没有运气。

I tried all sorts of solutions found on the web, without any luck.
I am using Angular 1.3.13 so the $cookies after $timeout is supposed to work, but doesn't.
I tried the withCredentials = true trick, both in the $httpProvider.defaults and in the $http call's config itself: no luck.

$ cookies似乎在每个范围内死亡和重置。

$cookies seems to die and reset within each scope.

例如:

$http({
    method: 'POST',
    url: 'http://distantsite.com/login.php',
    data: 'username=test&password=test',
    headers: {
      'Accept': 'text/html',
      'Cookie': "site_session=abcd", // this will be blocked: refused to set unsafe header
      'Content-Type': 'application/x-www-form-urlencoded',
      'Access-Control-Allow-Credentials': true,
    },
    withCredentials: true,
}).success(function(response, status, headers){
    $cookies.myTest1 = 'test1';
    $log.info('cookies: ', $cookies); // Object {myTest1: "test1"} 
    $timeout(function(){
        $cookies.myTest2 = 'test2';
        $log.info('cookies after timeout: ', $cookies); // Object {myTest2: "test2"}, myTest1 has disappeared!
    });
});

输出:

cookies:Object {myTest1: test1}

超时后的cookie:Object {myTest2:test2}

我没有得到任何真正的饼干,只有我设置的最后一个...

I don't get any of the real cookies, only the last one I set...

我完全不用解决方案。任何人都有一个有效的,工作的方式来检索在远程(跨域)网站的呼叫的Cookie从Cordova应用程序中的Angular?

如果可能,我想保留$ http

I'm completely out of solutions. Does anyone have a valid, working way to retrieve the cookies of a call to a remote (cross-domain) site from Angular in a Cordova appliation?
If possible, I'd like to keep $http for that, but I don't mind using something else as long as it works from client-side (browser).

推荐答案

我可以在客户端确认Cookie无法从Android的PhoneGap / Cordova中的JavaScript XMLHttpRequest对象管理,无论使用什么框架(因此不是角度问题)。

它似乎是一个功能 ,而不是一个错误,没有计划将cookie公开到JavaScript端(cookies 是托管的,只是没有公开到Cordova的JavaScript客户端)。

在iOS上似乎有一个解决方法,但我无法确认。

I confirm that cookies are not manageable from JavaScript's XMLHttpRequest object in PhoneGap/Cordova on Android, regardless of the framework used (so not an angular issue).
It seems to be a feature, not a bug, with no plans to expose the cookies down to the JavaScript side (the cookies are managed but just not exposed to the JavaScript client-side of Cordova).
There seem to be a workaround on iOS but I cannot confirm.

我发现Android的唯一解决方法是创建一个Cordova插件本地HTTP调用(使用Android上的java.net。*),访问Cookie并将其传递回JavaScript端。

查看Cordova插件文档以获取更多详细信息: http://docs.phonegap.com/en/4.0.0/guide_hybrid_plugins_index.md.html a>

The only workaround I found for Android is to create a Cordova plugin that performs the HTTP calls natively (using java.net.* on Android), giving access to the cookies and passing them back to the JavaScript side.
Check Cordova plugins documentation for more details: http://docs.phonegap.com/en/4.0.0/guide_hybrid_plugins_index.md.html

幸运的是,Cordova插件非常容易设置,而且非常强大,因为您可以获得所有的本机可能的功能。

Luckily, Cordova plugins are quite easy to setup and very powerful as you get all the natively possible features.

这篇关于读/写来自XHR请求/响应的Cookie(PhoneGap / Cordova)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆