Javascript Safari扩展中带有特殊字符的XMLHttpRequest不起作用
我正在尝试对Safari进行扩展,以使用的API进行身份验证。对于所有不包含特殊字符的密码,它都能正常工作,例如: å、ä、ö和空间 我现在有以下代码正在工作:Javascript Safari扩展中带有特殊字符的XMLHttpRequest不起作用,javascript,safari,xmlhttprequest,special-characters,safari-extension,Javascript,Safari,Xmlhttprequest,Special Characters,Safari Extension,我正在尝试对Safari进行扩展,以使用的API进行身份验证。对于所有不包含特殊字符的密码,它都能正常工作,例如: å、ä、ö和空间 我现在有以下代码正在工作: var xmlHttp = new XMLHttpRequest(); var url = "https://api.myphotodiary.com/user_status.json?api_key=66217f993f25a8a05dd72970473aa2227450dcad"; var username = "iphone"
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>
不确定,也没有时间深入研究,但编码字符串可能会有所帮助
var username = encodeURIComponent("blpninja"), //=> sends: blpninja
password = encodeURIComponent("./ hejä"); //=> sends: .%2F%20hej%C3%A4
在服务器端,它可能已经被解码了(
decodeURIComponent
)我刚刚通过encodeURIComponent(密码)解决了这个问题,然后使用xmlHttpRequest.open()Thx中的内置auth函数!很高兴我能帮上忙。克拉德卡卡会很好;-)
var username = encodeURIComponent("blpninja"), //=> sends: blpninja
password = encodeURIComponent("./ hejä"); //=> sends: .%2F%20hej%C3%A4