ios phonegap:基于 cookie 的身份验证 (PHP) 不起作用 [webview]

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/3709315/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-30 17:43:19  来源:igfitidea点击:

phonegap: cookie based authentication (PHP) not working [webview]

ajaxcookiesioswebviewcordova

提问by meaku

I'm working on a mobile web-app using sencha touch, HTML5 and phonegap as a wrapper.

我正在使用 sencha touch、HTML5 和 phonegap 作为包装器开发移动网络应用程序。

I'm using PHP-Authentication (Cookie) and ajax-requests. Everything works fine on safari or chrome, but after the deployment with phonegap (webview) it does't work anymore...

我正在使用 PHP 身份验证(Cookie)和 ajax 请求。在 safari 或 chrome 上一切正常,但在使用 phonegap (webview) 部署后,它不再工作了......

Any help would be appreciated :)

任何帮助,将不胜感激 :)

Some more details:

更多细节:

All data for my app is loaded via ajax requests to my server component "mobile.php". I use basic PHP-Auth to autenticate the user:

我的应用程序的所有数据都通过 ajax 请求加载到我的服务器组件“mobile.php”。我使用基本的 PHP-Auth 来验证用户:

  1. AJAX-Request [username, password] -> mobile.php -> Session established (cookie)
  2. All other requests if auth was successful
  1. AJAX-Request [用户名,密码] -> mobile.php -> 会话建立(cookie)
  2. 如果身份验证成功,则所有其他请求

What's the difference between a normal safari website and the webview?

普通的 safari 网站和 webview 有什么区别?

采纳答案by meaku

i figured it out:

我想到了:

you have to change the phonegap_delegate.m file and add the following to the init method:

您必须更改 phonegap_delegate.m 文件并将以下内容添加到 init 方法中:


- (id) init
{   
    /** If you need to do any extra app-specific initialization, you can do it here
     *  -jm
     **/
    //special setting to accept cookies via ajax-request
    NSHTTPCookieStorage *cookieStorage = [NSHTTPCookieStorage 
                                          sharedHTTPCookieStorage]; 
    [cookieStorage setCookieAcceptPolicy:NSHTTPCookieAcceptPolicyAlways]; 

    return [super init];
}

it enables webview to accept cookies from ajax requests

它使 webview 能够接受来自 ajax 请求的 cookie

回答by NAD

If your Phonegap AJAX requests are not firing callbacks like they're supposed to, this may be the reason.

如果您的 Phonegap AJAX 请求没有像预期的那样触发回调,这可能是原因。

If the response you're getting attempts to set cookies and you haven't done Michael's fix then your (jquery) AJAX request will fail quietly -- neither success: nor error: callbacks will fire despite the fact that the server actually received the request and sent a response. It appears you must do this even if you don't care about the cookies.

如果您收到的响应尝试设置 cookie 并且您还没有完成 Michael 的修复,那么您的 (jquery) AJAX 请求将安静地失败——既不是成功:也不是错误:尽管服务器实际收到了请求,回调仍将触发并发送了回复。即使您不关心 cookie,您似乎也必须这样做。

I hope this helps someone.

我希望这可以帮助别人。

I didn't care about the cookies but just spent a few hours trying to figure out why the callbacks didn't fire!

我并不关心 cookie,只是花了几个小时试图找出回调没有触发的原因!

回答by Ferdinand.kraft

There is a solution that works on android too:

也有一个适用于 android 的解决方案:

Install plugin https://github.com/wymsee/cordova-HTTPto perform arbitrary HTTP(S) requests.

安装插件https://github.com/wymsee/cordova-HTTP以执行任意 HTTP(S) 请求。

Replace XMLHttpRequestwith the plugin alternative (cordovaHTTP.getor cordovaHTTP.post):

替换XMLHttpRequest为插件替代(cordovaHTTP.getcordovaHTTP.post):

cordovaHTTP.post("https://example.com/login", {email: '[email protected]', passwd: "s3cr3t"}, {}, function(response) {
    console.log('success');
    console.log(response);
}, function(response) {
    console.log('failure');
    console.log(response);
});

The response will contain status, data and response.headers["Set-Cookie"], that can be parsed for name, value, domain, path and even HttpOnly flags ;-)

响应将包含状态、数据和response.headers["Set-Cookie"],可以解析名称、值、域、路径甚至 HttpOnly 标志;-)

Said cookie can be saved in LocalStorageand sent in subsequent requests (see cordovaHTTP.setHeader()or headerparameter of .get/.postmethods) to simulate an authenticated user on a desktop browser.

所述cookie可以被保存在LocalStorage与在随后的请求(见发送cordovaHTTP.setHeader()header的参数.get/.post方法)以模拟在桌面浏览器经过认证的用户。

回答by sunil

Best ways to store get and delete cookie its working fine in my app which is on live

存储 get 和 delete cookie 的最佳方法在我的应用程序中运行良好

To store value in cookie

在 cookie 中存储值

window.localStorage.setItem("key", "value");

To Get value in cookie

获取 cookie 中的值

var value = window.localStorage.getItem("key");

To Delete cookie value

删除 cookie 值

window.localStorage.removeItem("key");
window.localStorage.clear();