PhoneGap的iOS的Ajax请求无法完成 [英] Phonegap iOS ajax request never completes

查看:213
本文介绍了PhoneGap的iOS的Ajax请求无法完成的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个PhoneGap的应用程序,它连接到一个Web服务,身份验证使用HTTP基本身份验证。它使用的PhoneGap构建建成并针对Android和iOS。

I have a phonegap app which connects to a web service and authenticates using http basic authentication. It is built using phonegap build and targets Android and iOS.

在一个登录视图,对远程服务器Ajax请求触发检查证书是否正确,然后如果是这样,记录用户在主应用程序。

On a login view, an ajax request fires against the remote server to check if credentials are correct, then if so, logs the user in to the main application.

这时候成功部署到Android设备完成在台式机模拟器波动时也。

This completes successfully in ripple emulator on desktop pc and when also when deployed onto an Android device.

然而,当应用程序部署到iOS设备(iPod的触摸)的认证请求根本不完整的。使用PhoneGap的远程调试器,我可以看到Ajax请求启动,但永远不会完成。它始终处于待定状态。我使用jQuery AJAX的成功,错误和完整的处理程序的要求,但他们都不是以往任何时候都打,所以我没有得到机会看到从服务器返回的错误消息。请求似乎永远不会完成。

However, when the app is deployed onto an iOS device (ipod touch) the authentication request simply does not ever complete. Using phonegap remote debugger I can see that the ajax request starts but never completes. It is always in a pending state. I use jquery ajax success, error and complete handlers for the request, but none of them are ever hit so I don't get the chance to see any error messages returned from the server. The request never seems to complete.

我试图让Ajax请求到其他远程Web站点来测试我的应用程序可以沟通,他们成功了,所以它似乎并不好像我有白名单的问题。

I have tried making ajax requests to other remote web sites to test that my app can communicate and they succeed, so it doesn't seem as though I have white-listing issues.

什么样的​​问题可能是任何想法?

Any ideas of what the issue could be?

推荐答案

请阅读更新这个答案在底部。

原来的答复

我已经找到了问题是什么,并设法得到基本的认证工作。

I have found what the issue is and managed to get basic authentication working.

这个问题是Web服务器期待的基本身份验证信息是preemptively发送的要求。

The issue was that the web server was expecting basic authentication details to be preemptively sent with the request.

要做到这一点使用标题,如下图所示的jQuery AJAX的属性:

To do this use the 'headers' property of jquery ajax as shown below:

$.ajax
({
  type: "GET",
  url: "https://webserver/authenticate",
  headers: {
    "Authorization": "Basic " + btoa(USERNAME + ":" + PASSWORD)
  }
})
.done(function(){
    alert('Authenticated!')
})
.fail(function(){
    alert('Error!')
});

请参阅相关的答案: <一href="http://stackoverflow.com/a/11960692/1463497">http://stackoverflow.com/a/11960692/1463497

更新

我发现到底存在于iOS的Web视图使用基本身份验证没有可靠的方法。即一切都很好,如果输入正确的凭据,但是当他们不与401质询响应走来,iOS的Web视图似乎无法处理它。

I found in the end that there is no reliable way of using basic authentication in a web view in iOS. I.e everything is fine if correct credentials are entered but when they are not and the 401 challenge response comes along, iOS web view can't seem to handle it.

在最后,Web服务认证是通过传递用户名和密码参数作为URL的一部分来实现:

In the end, the web service authentication was implemented by simply passing 'username' and 'password' parameters as part of the url:

url: "https://webserver/authenticate?username=abc&password=123"

这是唯一一致的方式,我发现越来越认证横跨iOS和Android在Web视图工作(通过摆脱基本身份验证的完全)的。当然,这意味着更新的Web服务本身接受这样的认证。

This is the only consistent way I found of getting authentication to work across iOS and Android in a web view (by getting rid of basic authentication altogether). This of course meant updating the web service itself to accept authentication in this way.

这篇关于PhoneGap的iOS的Ajax请求无法完成的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆