【问题标题】:Ionic http get request (with Ionic) returns html dataIonic http get 请求(带 Ionic)返回 html 数据
【发布时间】:2016-02-25 18:31:21
【问题描述】:

我正在使用 Ionic 并像这样发出http 请求:

$http.get('//GLOBAL.IP/?username=' + username + '&content_type=json')
            .then(function (result) {
                $scope.days = [];

                alert(JSON.stringify(result));

                for (first in result.data.weeks) break;

                var currentWeek = result.data.weeks[first];
                var time = currentWeek.overall.as_time;

                $scope.week = currentWeek;
                for (day in currentWeek.days.sort().reverse()) {
                    if (day < 2) {
                        continue;
                    }
                    var currentDay = currentWeek.days[day];
                    $scope.days.push(currentDay);

                    if (currentDay.present) {
                        console.log(parseInt(currentDay.work_time_balance) + parseInt(currentWeek.overall.seconds));
                    }

                }
            }, function (result) {

                alert(JSON.stringify(result));

                $ionicPopup.alert({
                    title: 'Error',
                    template: 'Connection failed, try again later!'
                });
            })
            .finally(function () {
                // Stop the ion-refresher from spinning
                $scope.$broadcast('scroll.refreshComplete');
            });

当我使用命令ionic serve 在浏览器中打开应用程序时,一切似乎都正常,我已经修复了CORS 和其他东西。 但是当在移动应用程序中,我得到了这个(抱歉没有提供明文)

更新

        $http.get('//GLOBAL.IP/?username=' + username + '&content_type=json')
            .then(function (result) {
                alert('Good');
            }, function (result) {
                alert('Bad');
            })

这段代码返回给我GOOD,但result.data 仍然带有脚本标签。

【问题讨论】:

    标签: angularjs ionic-framework


    【解决方案1】:

    当您在工作模式下测试此应用时,localhost 是什么,它是在同一台机器上运行的本地服务器,对吧。

    当您在移动设备上安装或运行应用程序时,它所指向的位置?你知道吗,如果代码没问题,那么用你机器的 IP 替换 localhost 应该可以解决这个问题。

    【讨论】:

    • 嘿,是的,我会修改我的问题,实际上ip是“全局”并且http请求告诉我它是成功的,只是“数据”很奇怪。
    • 抱歉,您的回答并没有解决问题,所以它不是真正的“答案”
    【解决方案2】:

    在ionic http请求响应中有很多script标签带有addRow,是因为uri开头的//,协议在浏览器中选择成功,但在编译的应用程序中没有。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2021-11-01
      • 1970-01-01
      • 2016-10-16
      • 2022-01-20
      • 1970-01-01
      • 2021-08-13
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多