使用 XMLHTTPRequest 进行基本身份验证 [英] Basic Authentication With XMLHTTPRequest

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

问题描述

我正在尝试使用 XMLHTTPRequest 在 twitter 上获取更新.

I am attempting to use XMLHTTPRequest to get an update on twitter.

var XMLReq = new XMLHttpRequest();
XMLReq.open("GET", "http://twitter.com/account/verify_credentials.json", false, "TestAct", "password");
XMLReq.send(null);

但是,使用我的嗅探器,我看不到任何授权标头正在通过.因此,我收到了来自 Twitter 的 401 错误响应.

However, using my sniffer I cannot see any authorization headers being passed through. Hence, I get a 401 error response from Twitter.

帐号和密码输入正确.

有人尝试过吗?谁能给我一些指示?谢谢.

Anyone attempt this? Can anyone give me some pointers? Thank you.

推荐答案

你只需要在base64编码的字符串中添加一个Authorization header,一个用户名和密码,如下所示.

You just need to add a Authorization header, an user name and password in a base64 encoded string as follows.

XMLReq.setRequestHeader("Authorization", "Basic " + btoa("username:password"));

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

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