[英]Phonegap + Sencha + Android : ajax request with a basic access authentication failled
I folks, 我伙计,
I'm working on a Phonegap application and the Sencha framework. 我正在研究Phonegap应用程序和Sencha框架。
I try to reach a protected server but the authentication failed with Android (but not with iOS). 我尝试访问受保护的服务器但使用Android验证失败(但不是iOS)。 I use the code below :
我使用下面的代码:
Ext.Ajax.request({
url:"http://user:password@api.website.fr/query.json",
method: 'GET',
// I tried to send the header directly but it didn't work too
headers: {
"Authorization": "Basic s2dh3qs76jd2hqjsdh=="
},
success: function (result, request) {
alert(result);
},
failure: function ( result, request) {
for(var key in result)
alert(result[key]);
}
});
The error message says me that an HTTP digest is required... 错误消息告诉我需要HTTP摘要...
Just to know: 想知道:
alert(result[key])
alert(result[key])
Thank you guys, you all rock! 谢谢你们,你们都摇滚!
I agree with @Mariano. 我同意@Mariano。 Why can't you try it in the browser and then deploy as an app with phonegap.
为什么你不能在浏览器中尝试它,然后部署为带有phonegap的应用程序。 The cross-domain problem can be solved by starting google chrome from the terminal by this command google-chrome --args --disable-web-security
可以通过此命令从终端启动谷歌浏览器来解决跨域问题google-chrome --args --disable-web-security
Check out this link for more information 有关更多信息,请查看此链接
http://www.senchatouchbits.com/7/cross-domain-ajax-requests.html http://www.senchatouchbits.com/7/cross-domain-ajax-requests.html
I had endless trouble with authenticating my ajax requests but I finally got it to work. 我在验证我的ajax请求时遇到了无穷无尽的麻烦,但我终于让它工作了。 I got a basic base64_encode algorithm of the net.
我得到了网络的基本base64_encode算法。 Then all you need to to is adjust your header as follows
然后您需要调整标题,如下所示
Ext.Ajax.request({
url:'yourwebserviceurl',
method:'GET',
headers: {
Authorization: 'Basic '+ base64_encode(form.username+':'+form.password)
},
success: function(response){
//Ext.Msg.alert(response.responseXML);
console.log(response.responseText);
},
failure: function(response){
//console.log('Success response: ' + response.responseText);
Ext.Msg.alert('b');
}
});
This worked for a basic soap service ! 这适用于基本肥皂服务!
I have the same problem... where you able to fix it? 我有同样的问题......你能解决它吗?
Btw... you have to set the user and password like this: 顺便说一句......你必须像这样设置用户名和密码:
Ext.Ajax.request({
url:"http://api.website.fr/query.json",
method: 'GET',
username : 'user',
password : 'password',
// I tried to send the header directly but it didn't work too
headers: {
"Authorization": "Basic s2dh3qs76jd2hqjsdh=="
},
success: function (result, request) {
alert(result);
},
failure: function ( result, request) {
for(var key in result)
alert(result[key]);
}
});
Use console.log to see what a js object have, you allways must try the app in a browser with a debbuger like Firebug to detect issues more easy. 使用console.log来查看js对象的内容,你总是必须在浏览器中使用像Firebug这样的debbuger来尝试应用程序,以便更轻松地检测问题。
To your problem in Android, make sure this line is in phonegap app manifest file: < uses-permission android:name="android.permission.INTERNET" >< /uses- permission > 对于Android中的问题,请确保此行位于phonegap app清单文件中:<uses-permission android:name =“android.permission.INTERNET”> </ uses- permission>
Good Luck! 祝好运!
This is a cross domain request problem. 这是跨域请求问题。 You have to modify your code to create a JSONP request
您必须修改代码才能创建JSONP请求
Ext.util.JSONP.request({
url: 'http://url',
callbackKey: 'callback',
callbackName: 'callback',
model: 'Model',
params: {},
callback: function(result){
var shows = result.data;
},
});
Keep in mind that you have to adapt also the server side, so that the respone for it is a jsonp result (callback) 请记住,您还必须适应服务器端,以便它的响应是一个jsonp结果(回调)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.