bla*_*k3r 7 rest jquery basic-authentication cordova
我有一个带有jQuery 1.9.1的phonegap应用程序,只要用户名中没有"@"符号(比如电子邮件地址),就可以了.它只在iOS上失败.
我怀疑它可能不是urlencoding @符号或其他东西.
Android,运行正常.
$.ajax({
url: "https://" + this.hostname + "/alertusmw/services/rest/" + endPoint,
type: method,
dataType: 'json',
contentType: 'application/com.alertus-v1.0+json',
cache:false,
username: this.username,
password: this.password,
beforeSend: function (xhr) {
xhr.setRequestHeader("Authorization",
"Basic " + $.base64.encode(this.username + ":" + this.password));
},
data: options.data
}).done(function(response) {
console.log("DONE: " + method + ' completed: ');
console.log(response);
options.success( response );
})
.fail(function(jqXHR, textStatus, errorThrown) {
console.log("FAIL: " + method + " FAILED: " + textStatus + "\n" + "ERROR THROWN: " + errorThrown);
console.log("jqXHR thing: ", jqXHR);
options.error(jqXHR,textStatus,errorThrown);
})
.always(function(jqXHR, textStatus, errorThrown) {
console.log("In the always", jqXHR, textStatus, errorThrown);
});
Run Code Online (Sandbox Code Playgroud)
如果用户名没有'@',那么再次完美运行
我怀疑它是url编码的原因是它是否将它发布为:
https://user@domain:password@blah.domain.com,浏览器可能不会将该domain:password部分包含为主机(因为第一个@是分隔用户:传递域名...
以下是我对此的了解:

^ - 我认为base64编码的全部意义完全是为了避免特殊字符导致问题...所以我认为这可能是Chrome有用...
相关SO帖子: - 基本身份验证在cordova ios中失败(没有答案,略有不同)
尝试在base64 编码之前包装encodeURIComponent()。
beforeSend: function (xhr) {
xhr.setRequestHeader("Authorization",
"Basic " + $.base64.encode(encodeURIComponent(this.username + ":" + this.password)));
},
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
1380 次 |
| 最近记录: |