执行 cordova emulate 时无法连接到服务器
Cant connect to server when execute cordova emulate
我使用示例 LDAP Login with LDAP Server 将我的应用程序与 LDAP 连接。我可以将应用程序示例与服务器连接。我也可以修改应用程序并连接。
问题是当我将 mfp 作为插件时,将示例放入 iOS 的 Cordova 项目中。
我可以在 _MobileBrowserSimulator 中看到应用程序并且可以连接到服务器但是当我执行 mfp cordoba emulate 应用程序时不连接服务器。
---更新---
我使用与 LDAP 相同的示例,但是这个 运行s 在移动模拟器中,但在设备中不起作用。这是一个简单的例子,在模拟器中 运行 但在设备中没有。
main.js
function getSecretData(){
var request = new WLResourceRequest("/Random/getInteger",
WLResourceRequest.GET);
request.send().then(onSuccess, onFailure);
}
function onSuccess(r) {
document.getElementById('guille').innerHTML = r;
}
function onFailure(r) {
document.getElementById('guille').innerHTML = r;
}
index.js
function wlCommonInit(){
WL.App.setServerUrl("http://127.0.0.1:10080/MyMFPProject",success, fail);
WL.Client.connect();
}
function success(r){
document.getElementById('guille').innerHTML = r;
}
function fail(r){
document.getElementById('guille').innerHTML = "error: " +r;
}
function onSuccess(r) {
document.getElementById('guille').innerHTML = JSON.stringify(r);
}
function onFailure(r) {
document.getElementById('guille').innerHTML = JSON.stringify(r);
}
更新:根据评论,听起来 ATS 仍处于启用状态,其中请求确实会失败。通过将以下内容添加到应用程序的 *-info.plist:
来禁用 ATS
<key>NSAppTransportSecurity</key>
<dict>
<key>NSAllowsArbitraryLoads</key><true/>
</dict>
在此处阅读更多内容:https://developer.ibm.com/mobilefirstplatform/2015/09/09/ats-and-bitcode-in-ios9/
代码在我看来很脆弱。
我会像下面这样改变它。如果它仍然会失败,那么您需要创建一个失败的测试用例演示。听起来它也应该在没有任何与 LDAP 相关的情况下失败...
function wlCommonInit(){
WL.App.setServerUrl("http://127.0.0.1:10080/MyMFPProject",success, fail);
}
function success(r){
document.getElementById('guille').innerHTML = r;
WL.Client.connect({onSuccess: connectSuccess, onFailure: connectFailure);
}
function connectSuccess() {
getSecretData();
// ...
}
function connectFailure() {
// handle connect failure
}
// the rest of the functions
我使用示例 LDAP Login with LDAP Server 将我的应用程序与 LDAP 连接。我可以将应用程序示例与服务器连接。我也可以修改应用程序并连接。
问题是当我将 mfp 作为插件时,将示例放入 iOS 的 Cordova 项目中。
我可以在 _MobileBrowserSimulator 中看到应用程序并且可以连接到服务器但是当我执行 mfp cordoba emulate 应用程序时不连接服务器。
---更新---
我使用与 LDAP 相同的示例,但是这个 运行s 在移动模拟器中,但在设备中不起作用。这是一个简单的例子,在模拟器中 运行 但在设备中没有。
main.js
function getSecretData(){
var request = new WLResourceRequest("/Random/getInteger",
WLResourceRequest.GET);
request.send().then(onSuccess, onFailure);
}
function onSuccess(r) {
document.getElementById('guille').innerHTML = r;
}
function onFailure(r) {
document.getElementById('guille').innerHTML = r;
}
index.js
function wlCommonInit(){
WL.App.setServerUrl("http://127.0.0.1:10080/MyMFPProject",success, fail);
WL.Client.connect();
}
function success(r){
document.getElementById('guille').innerHTML = r;
}
function fail(r){
document.getElementById('guille').innerHTML = "error: " +r;
}
function onSuccess(r) {
document.getElementById('guille').innerHTML = JSON.stringify(r);
}
function onFailure(r) {
document.getElementById('guille').innerHTML = JSON.stringify(r);
}
更新:根据评论,听起来 ATS 仍处于启用状态,其中请求确实会失败。通过将以下内容添加到应用程序的 *-info.plist:
来禁用 ATS<key>NSAppTransportSecurity</key>
<dict>
<key>NSAllowsArbitraryLoads</key><true/>
</dict>
在此处阅读更多内容:https://developer.ibm.com/mobilefirstplatform/2015/09/09/ats-and-bitcode-in-ios9/
代码在我看来很脆弱。
我会像下面这样改变它。如果它仍然会失败,那么您需要创建一个失败的测试用例演示。听起来它也应该在没有任何与 LDAP 相关的情况下失败...
function wlCommonInit(){
WL.App.setServerUrl("http://127.0.0.1:10080/MyMFPProject",success, fail);
}
function success(r){
document.getElementById('guille').innerHTML = r;
WL.Client.connect({onSuccess: connectSuccess, onFailure: connectFailure);
}
function connectSuccess() {
getSecretData();
// ...
}
function connectFailure() {
// handle connect failure
}
// the rest of the functions