Safari 扩展中带有特殊字符的 XMLHttpRequest 不起作用
我正在尝试对 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
不确定,也没有时间深入研究它,但也许对字符串进行编码会有帮助?
在服务器端,它可能已被解码(
decodeURIComponent
)Not sure and no time to delve into it, but maybe encoding the strings will help?
on the server side it may have do be decoded (
decodeURIComponent
)