Safari 扩展中带有特殊字符的 XMLHttpRequest 不起作用

发布于 2024-09-06 13:18:04 字数 1599 浏览 4 评论 0原文

我正在尝试对 Safari 进行扩展,以使用 http://myphotodiary.com 的 API 进行身份验证。对于所有不包含特殊字符的密码,它效果很好,例如:

å、ä、ö 和空间

我现在可以使用以下代码:

var xmlHttp = new XMLHttpRequest();

var url = "https://api.myphotodiary.com/user_status.json?api_key=66217f993f25a8a05dd72970473aa2227450dcad";

var username = "iphone";
var password = "bdbiphone123";

var authy = Base64.encode(username+":"+password);

xmlHttp.open('GET', url, true);
xmlHttp.setRequestHeader("Authorization", "Basic " + authy);
xmlHttp.onreadystatechange = function(){
if (xmlHttp.readyState == 4){
    var data;
    eval("data="+xmlHttp.responseText);
    console.log(data);
}
};
xmlHttp.send(null);

该代码适用于所有不包含特殊字符的密码,但如果我将登录信息更改为

var username = "blpninja";
var password = "./ hejä";

它,它就会开始失败。

这段代码在 Safari 中有效,但当我将其放入扩展中时则无效。

我还尝试了 xmlHttpRequest.open 内置身份验证,

xmlHttp.open('GET', url, true, username, password);

结果相同。

这是我的全局页面的头部:

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd">
<html>
<head>

<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">

</head>

我收到“无法加载资源:已取消”,这些是 Safari 发送的标头:

Authorization:Basic YmxwbmluamE6Li8gaGVqw6Q=
Content-Type:text/xml; charset=utf-8
User-Agent:Mozilla/5.0 (Macintosh; U; Intel Mac OS X 10_6_4; sv-se) AppleWebKit/533.16 (KHTML, like Gecko)

Im trying to make an extension to Safari that authenticates with http://myphotodiary.com's API. And it works well, for all passwords that dont include special characters like:

å,ä,ö and space

I have the following code working now:

var xmlHttp = new XMLHttpRequest();

var url = "https://api.myphotodiary.com/user_status.json?api_key=66217f993f25a8a05dd72970473aa2227450dcad";

var username = "iphone";
var password = "bdbiphone123";

var authy = Base64.encode(username+":"+password);

xmlHttp.open('GET', url, true);
xmlHttp.setRequestHeader("Authorization", "Basic " + authy);
xmlHttp.onreadystatechange = function(){
if (xmlHttp.readyState == 4){
    var data;
    eval("data="+xmlHttp.responseText);
    console.log(data);
}
};
xmlHttp.send(null);

This code works great for all passwords that dont include the special characters, but if i change the login info to

var username = "blpninja";
var password = "./ hejä";

it starts to fail.

This exact code works in Safari, but not when i put it in an extension.

I have also tried the xmlHttpRequest.open built in authentication

xmlHttp.open('GET', url, true, username, password);

with the same result.

This is my head of the global page:

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd">
<html>
<head>

<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">

</head>

I get the "Failed to load resource: cancelled" and these are the headers Safari is sending:

Authorization:Basic YmxwbmluamE6Li8gaGVqw6Q=
Content-Type:text/xml; charset=utf-8
User-Agent:Mozilla/5.0 (Macintosh; U; Intel Mac OS X 10_6_4; sv-se) AppleWebKit/533.16 (KHTML, like Gecko)

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

—━☆沉默づ 2024-09-13 13:18:04

不确定,也没有时间深入研究它,但也许对字符串进行编码会有帮助?

var username = encodeURIComponent("blpninja"), //=> sends: blpninja
    password = encodeURIComponent("./ hejä");  //=> sends: .%2F%20hej%C3%A4

在服务器端,它可能已被解码(decodeURIComponent

Not sure and no time to delve into it, but maybe encoding the strings will help?

var username = encodeURIComponent("blpninja"), //=> sends: blpninja
    password = encodeURIComponent("./ hejä");  //=> sends: .%2F%20hej%C3%A4

on the server side it may have do be decoded (decodeURIComponent)

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文