当基本授权用户名有@符号(ios / cordova)时,Jquery .ajax失败 [英] Jquery .ajax fails when basic auth username has @ symbol (ios / cordova)
问题描述
我有一个phonegap应用程序w / jQuery 1.9.1
工作伟大,只要用户名没有@符号(如在电子邮件地址)。
它只在iOS上失败。
我怀疑它可能不是urlencoding @符号或某事。
- , 它从不命中任何回调(完成,失败或总是),Wireshark显示请求不到达服务器
$ .ajax({
url:https://+ this。 hostname +/ alertusmw / services / rest /+ endPoint,
type:method,
dataType:'json',
contentType:'application / com.alertus-v1.0 + ',
cache:false,
username:this.username,
password:this.password,
beforeSend:function(xhr){
xhr.setRequestHeader(授权,
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 +\\\
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);
}
- 在iphone和chrome桌面上验证base64标题是否相同。 li>
- 这不是SSL证书问题。
- 这不是cors问题
如果用户名没有'@' p>
我怀疑它是url编码的原因是如果它是发布它:
https:// user @ domain:password @ blah.domain.com
,浏览器不会包含 domain:password
部分作为主机(因为第一个@是什么分隔用户:pass from the domain ...
这里有什么告诉我:
^ - 我认为base64编码正是为了避免特殊字符导致问题...所以我认为也许这是铬是有帮助的...
相关的SO帖子:
- 基本验证在cordova ios中失败(没有答案,略有不同)
我会打赌问题是不使用 contentType:application / x-www-form-urlencoded
无论如何,你应该真正的调试你的Webview的真实设备,在Safari控制台上查找xhr错误。如果您不熟悉Safari远程调试,很容易:
- 在您的iPhone / iPad中,转到设置 - > Safari - >高级=>启用Web检查器。
- 通过电缆连接到MacOSX,然后从桌面的Safari的开发菜单中选择您的应用程序 检查与您的请求有关的任何错误,或者更好地逐步调试代码和回调。
I have a phonegap app w/ jQuery 1.9.1 Worked great as long as the username doesn't have '@' symbol in it (like in email addresses). It only fails on iOS.
I suspect it's probably not urlencoding the @ sign or something.
- iPhone, it never hits any callbacks (done, fail or always), Wireshark shows request doesn't get to the server.
- Chrome Desktop: works fine.
Android, works fine.
$.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); });
- Verified the base64 header is identical on iphone and chrome desktop.
- It's not an ssl cert issue.
- It's not a cors issue
- It's not an invalid user/pass
Again works perfectly if username doesn't have an '@'
The reason I suspect it's something with url encoding is if it was posting it as:
https://user@domain:password@blah.domain.com
, the browser wouldn't probably include the domain:password
part as the host (since the first @ is what separates user:pass from the domain...
Here's what clued me in to this:
^-- I thought the entire point of base64 encoding was exactly to avoid special characters causing issues... so I thought that maybe this was chrome being helpful...
Related SO Posts: - Basic Authentication fails in cordova ios (no answers, slightly different)
I would bet the problem is not using a contentType : "application/x-www-form-urlencoded"
.
Anyway, you should definitely debug your Webview against a real device, to look for xhr errors on the Safari console. If you are not familiar with Safari remote debugging, it's easy:
- in your iPhone/iPad, go to Settings -> Safari -> Advanced => Enable Web Inspector.
- connect to MacOSX via cable, and select your app from the "Develop" menu of Safari in your desktop
- Now check any errors regarding your request, or better yet, debug the code and callbacks step by step.
这篇关于当基本授权用户名有@符号(ios / cordova)时,Jquery .ajax失败的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!