Ionic http get request (with Angular) returns html data

I am making http request like this:

$http.get('//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');
            });

While I am opening app in browser with command ionic serve, everything seems working okay, I’ve already fixed CORS and other stuff.
But when In mobile app, I am getting this (sorry for not providing plaintext)

enter image description here

UPDATE

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

This code returns me GOOD, but result.data is still with script tags.

so like … what do you get when you open the url in the browser

//IP/?username=' + username + '&content_type=json

Seems like you’re using a protocol relative url (//) to your backend. This will work in the browser using ionic serve, but not in a mobile app.
Replace it with http:// (or https:// ) and check the results.
Personally I would replace the alert statements with console.log statements. Alerts are blocking, and console.log will allow you to log complete objects to the developer console. Easier for debugging.

2 Likes

Solved! Thank you very much!

so what did you do to solve it?

As @tomkuipers it was protocol problem because of //