XMLHttpRequest无法通过基本身份验证 [英] XMLHttpRequest fails basic authentication

查看:103
本文介绍了XMLHttpRequest无法通过基本身份验证的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

任何想法,为什么XMLHttpRequest Pebble JS Framework 无法在Android上进行基本身份验证,但可以在iOS上运行?

Any idea why XMLHttpRequest with correct credentials in Pebble JS Framework fails basic authentication on Android but works in iOS?

完全相同的代码,大致如下:

Exactly the same code, along the lines of:

var req = new XMLHttpRequest();
req.open(method, url, true, user, pass);
req.send(data);
req.onreadystatechange = function() { ... }

从Android Pebble应用返回401,但在iOS中正确进行身份验证.

Returns 401 in from Android Pebble app, but authenticates correctly in iOS.

推荐答案

我找到了一种适用于Android的变通方法.

I found a workaround that worked for me on Android.

不知道为什么,但是直接通过身份验证的请求:

Don’t know why but direct authenticated request:

    req.open(method, fullurl, true, user, pass);
    req.send(data);

对我不起作用–它总是返回401.因此,我改为尝试通过标头设置基本身份验证:

didn’t work for me – it always returned 401. So Instead I tried to set basic authentication via header:

    req.open(method, fullurl, true);
    req.setRequestHeader("Authorization", "Basic " + Base64.encode(user + ":" + pass)); 
    req.send(data);

(从此处获取Base64: https://stackoverflow.com/a/246813/961695 )–它奏效了!也许在Android上的XmlHttpRequest实现中存在错误.

(where Base64 is taken from here: https://stackoverflow.com/a/246813/961695) – and it worked! Perhaps there’s a bug in XmlHttpRequest implementation on android.

这篇关于XMLHttpRequest无法通过基本身份验证的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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