在模拟ionic应用程序时,无法使用http服务连接到服务器[英] Cannot connect to server using http service when emulating ionic application

本文是小编为大家收集整理的关于在模拟ionic应用程序时,无法使用http服务连接到服务器的处理/解决方法,可以参考本文帮助大家快速定位并解决问题,中文翻译不准确的可切换到English标签页查看源文。

问题描述

我已经构建了一个简单的离子应用程序来测试我的API.但是我遇到了问题,因为当我以实时弹奏模式(-l选项)运行它时,一切正常(离子运行iOS -L).但是,当我没有使用Live-Reload(Ionic Run IOS)时,似乎我的应用程序会出现错误.我无法调试我的应用程序出错的地方,因为这需要实时弹奏选项,而不会发生错误.

我已经重新安装了白名单插件几次.我在小部件区域内添加了以下行中的config.xml.

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

以及我的索引内的下线.html

<meta http-equiv="Content-Security-Policy" content="default-src *; style-src &apos;self&apos; &apos;unsafe-inline&apos;; script-src &apos;self&apos; &apos;unsafe-inline&apos; &apos;unsafe-eval&apos;">

这是我的控制器代码

var url="/api/event/new"

.controller('Test', function($http, $scope) {
    $http.get(url).then(function(data) {
        $scope.allEvents = {
            all : function() {
                return data.data;
            },
            remove: function(Events) {
                data.data.splice(data.data.indexOf(Events), 1);
            },
            get : function(_id) {
                for (var x in data.data) {
                    if(data.data[i]._id == _id) {
                        return data.data[i];
                    }
                }
                return null;
            }
        };
        $scope.isNotConnected = false;
    }, function(error) {
        $scope.allEvents = {
            all : function() {
                return [{"title" : "Connection error!"}];
            }
        }
        $scope.isNotConnected = true;
        console.log(error);
    });
})

这是我的离子信息

Cordova CLI: 6.1.1
Ionic Framework Version: 1.3.1
Ionic CLI Version: 1.7.15
Ionic App Lib Version: 0.7.2
ios-deploy version: 1.8.6 
ios-sim version: 5.0.8 
OS: Mac OS X El Capitan
Node Version: v5.9.1
Xcode version: Xcode 7.3 Build version 7D175 

推荐答案

使用远程调试-IOS和Safari

您可以避免使用实时重载并使用Safari调试来查看出了什么问题.

Safari可用于在连接的iOS设备上调试离子应用程序. 首先,我们需要在连接的设备上启用Web检查器.网络 可以在设置> Safari>"高级"下找到检查员.接下来,头 到Mac上的Safari,并在菜单中启用show开发菜单 在Safari>"首选项"下的酒吧>高级.连接的设备应 现在出现在"开发"菜单中.从那里,您可以检查并使用 Safari的开发人员工具来调试您的应用程序!

来源: >

本文地址:https://www.itbaoku.cn/post/1937748.html

问题描述

I have built a simple ionic app to test my api. But I ran into problem because when I run it in live-reload mode (-l option) everything works just fine (ionic run ios -l). But when I did not use live-reload (ionic run ios), it is seems that my app got an error. I cannot debug where my apps went wrong because that require live-reload option which an error won't occur.

I have reinstall the whitelist plugins several times. I added the below line into my config.xml inside widget zone.

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

and also the below line in inside my index.html

<meta http-equiv="Content-Security-Policy" content="default-src *; style-src &apos;self&apos; &apos;unsafe-inline&apos;; script-src &apos;self&apos; &apos;unsafe-inline&apos; &apos;unsafe-eval&apos;">

this is my controller code

var url="/api/event/new"

.controller('Test', function($http, $scope) {
    $http.get(url).then(function(data) {
        $scope.allEvents = {
            all : function() {
                return data.data;
            },
            remove: function(Events) {
                data.data.splice(data.data.indexOf(Events), 1);
            },
            get : function(_id) {
                for (var x in data.data) {
                    if(data.data[i]._id == _id) {
                        return data.data[i];
                    }
                }
                return null;
            }
        };
        $scope.isNotConnected = false;
    }, function(error) {
        $scope.allEvents = {
            all : function() {
                return [{"title" : "Connection error!"}];
            }
        }
        $scope.isNotConnected = true;
        console.log(error);
    });
})

This is my ionic info

Cordova CLI: 6.1.1
Ionic Framework Version: 1.3.1
Ionic CLI Version: 1.7.15
Ionic App Lib Version: 0.7.2
ios-deploy version: 1.8.6 
ios-sim version: 5.0.8 
OS: Mac OS X El Capitan
Node Version: v5.9.1
Xcode version: Xcode 7.3 Build version 7D175 

推荐答案

Use Remote Debugging - iOS and Safari

you can avoid using live reload and use safari debugging to see what's going wrong.

Safari can be used to debug an Ionic app on a connected iOS device. First, we need to enable Web Inspector on the connected device. Web Inspector can be found under Settings > Safari > Advanced. Next, head over to the Safari on your Mac and enable Show Develop menu in menu bar under Safari > Preferences > Advanced. The connected device should now appear in the Develop menu. From there, you can inspect it and use Safari's developer tools to debug your application!

Source: http://ionicframework.com/docs/cli/run.html