Ionic 3 Cordova ajax 调用在 Windows 10 (UWP) 上失败

问题描述 投票:0回答:1

我无法让我的 ajax 调用在为 Windows 10 UWP 构建的 Ionic 3 Cordova 应用程序上运行。他们可以访问本地主机,但不能访问任何外部连接。

该应用程序在 Android 和 iOS 上都运行良好。

我正在尝试在我的开发机器上进行本地测试。我使用(购买的)证书来签署应用程序,安装此证书,构建 Windows 应用程序,并且能够打开构建的

CordovaApp.Windows10_1.0.1.1_x86.appxupload
,然后双击嵌入的
CordovaApp.Windows10_1.0.1.1_x86.appx
文件进行安装,成功完成。安装表明应用程序需要互联网访问。

config.xml
中,我有以下标签,如其他地方所建议的......

 <allow-navigation href="*" />
 <access origin="*" />

但是,当我运行时,http.get 调用仅返回

0
,没有其他信息。我可以在 Visual Studio 中运行,查看返回的错误对象,除了这个
0
返回之外,没有获得更多信息。

我已经运行了fiddler,启用了https解密,如here所述,但我在响应标头中看到的是

HTTP/1.0 200 Connection Established
FiddlerGateway: Direct
StartTime: 13:44:21.686
Connection: close

主视图中的结果实际上显示了

200
,所以我认为这没有向我展示任何真实的东西。

我可能会错过什么?

当我像这里一样旁加载应用程序时,我是否应该能够在 Windows 10 计算机上使用外部 ajax?我还没有在商店尝试过。在我知道它有效之前我不想上传。

更新1

如果我在同一台计算机上运行应用程序,仅使用 Ionic 服务(因此它只在浏览器中运行而不是托管在 UWP 中),ajax 调用也可以正常工作。

更新2

我现在已经使用 Visual Studio 模板创建了一个 Cordova 应用程序,因此排除了所有其他框架。

我使用普通 JavaScript 来进行休息通话...

 document.addEventListener('deviceready', callUrl, false);

 function callUrl() {
  
  console.log('callUrl'); 
  var xhr = new XMLHttpRequest();
  xhr.open('GET', 'https://myserveraddress.com/myapp/testroute');
  xhr.send(null);

  xhr.onreadystatechange = function () {
    var DONE = 4; // readyState 4 means the request is done.
    var OK = 200; // status 200 is a successful return.
    if (xhr.readyState === DONE) {
      if (xhr.status === OK)
        console.log(xhr.responseText); 
    } else {
      console.log('Error: ' + xhr.status); 
    }
  }
 };

我在调试器中运行它,即使在这里我也收到错误(状态代码为 0)。

当我打开构建包并查看

cordova_plugins.js
文件时,我注意到另一件事..

我的 Ionic 应用程序有以下内容...

cordova.define('cordova/plugin_list', function(require, exports, module) {
    module.exports = [
        {
            "id": "cordova-plugin-console.logger",
            "file": "plugins/cordova-plugin-console/www/logger.js",
            "pluginId": "cordova-plugin-console",
            "clobbers": [
                "cordova.logger"
            ]
        },
        {
            "id": "cordova-plugin-console.console",
            "file": "plugins/cordova-plugin-console/www/console-via-logger.js",
            "pluginId": "cordova-plugin-console",
            "clobbers": [
                "console"
            ]
        },
        {
            "id": "cordova-plugin-device.device",
            "file": "plugins/cordova-plugin-device/www/device.js",
            "pluginId": "cordova-plugin-device",
            "clobbers": [
                "device"
            ]
        },
        {
            "id": "cordova-plugin-device.DeviceProxy",
            "file": "plugins/cordova-plugin-device/src/windows/DeviceProxy.js",
            "pluginId": "cordova-plugin-device",
            "merges": [
                ""
            ]
        },
        {
            "id": "cordova-plugin-splashscreen.SplashScreen",
            "file": "plugins/cordova-plugin-splashscreen/www/splashscreen.js",
            "pluginId": "cordova-plugin-splashscreen",
            "clobbers": [
                "navigator.splashscreen"
            ]
        },
        {
            "id": "cordova-plugin-splashscreen.SplashScreenProxy",
            "file": "plugins/cordova-plugin-splashscreen/www/windows/SplashScreenProxy.js",
            "pluginId": "cordova-plugin-splashscreen",
            "runs": true
        },
        {
            "id": "cordova-plugin-statusbar.statusbar",
            "file": "plugins/cordova-plugin-statusbar/www/statusbar.js",
            "pluginId": "cordova-plugin-statusbar",
            "clobbers": [
                "window.StatusBar"
            ]
        },
        {
            "id": "cordova-plugin-statusbar.StatusBarProxy",
            "file": "plugins/cordova-plugin-statusbar/src/windows/StatusBarProxy.js",
            "pluginId": "cordova-plugin-statusbar",
            "runs": true
        },
        {
            "id": "ionic-plugin-keyboard.KeyboardProxy",
            "file": "plugins/ionic-plugin-keyboard/src/windows/KeyboardProxy.js",
            "pluginId": "ionic-plugin-keyboard",
            "clobbers": [
                "cordova.plugins.Keyboard"
            ],
            "runs": true
        }
    ];
    module.exports.metadata = 
    // TOP OF METADATA
    {
        "cordova-plugin-console": "1.0.5",
        "cordova-plugin-device": "1.1.4",
        "cordova-plugin-splashscreen": "4.0.3",
        "cordova-plugin-statusbar": "2.2.2",
        "cordova-plugin-whitelist": "1.3.1",
        "ionic-plugin-keyboard": "2.2.1"
    };
    // BOTTOM OF METADATA
    });

现在,我注意到

module.exports.metadata
中的每个插件在
module.exports
除了
cordova-plugin-whitelist
中也有一个条目!

如果我为在 VS 中创建的 Corvoda 应用程序打开相同的文件,我会看到以下内容...

cordova.define('cordova/plugin_list', function(require, exports, module) {
    module.exports = [];
    module.exports.metadata = 
    // TOP OF METADATA
    {
        "cordova-plugin-whitelist": "1.2.2"
    };
    // BOTTOM OF METADATA
    }); 

所以这对于白名单插件来说也没有任何其他作用

这里会不会漏掉了什么?难道这个白名单插件没有正确安装?

ajax cordova uwp ionic3 visual-studio-cordova
1个回答
0
投票

我也遇到过类似的情况,我的 ajax 调用在 TEST 中运行良好,但当我转移到 PROD 时,它们会失败。

最终找到答案是因为我尝试访问的服务器上缺少中间证书。 TEST 有证书,PROD 没有。

我希望这有帮助。

© www.soinside.com 2019 - 2024. All rights reserved.