Unexpected token < in JSON at position 80 at JSON.parse (<anonymous>)


#1


Etiqueta <ion-content>
#2

Posting a screenshot of your desktop is one of the impolitest things you can do.

Please post the error message as text, include your ionic info output, what code you are running and what JSON the server is returning that causes this error.


#3

lo que muestra en la consola
Notice: Undefined index: Correo in C:\xampp\htdocs\final_slim\api\app\route\auth_route.php on line 13



Notice: Undefined index: Password in C:\xampp\htdocs\final_slim\api\app\route\auth_route.php on line 13

y la linea 13 lo que tengo en el json_encode

$app->group(’/auth/’, function () {
$this->post(‘autenticar’, function ($req, $res, $args) {
$parametros = $req->getParsedBody();

    return $res->withHeader('Content-type', 'application/json')
               ->write(
                 json_encode($this->model->auth->autenticar($parametros['Correo'], $parametros['Password']))
               );
});

});


#4

Your PHP is broken. $parametros[‘Correo’], $parametros[‘Password’] both do not exist, so the code doesn’t work.


#5

en el formulario si le quito la etiqueta funciona todo bien , este código lo e usado en otros proyectos y todo bien, pero cuando lo pase ionic v 1 me encontrado con este error .
este es el codigo de mi service que tiene toda la lógica para consumir mi REST API
gracias por la ayuda

angular.module(‘app.services’, [])

.factory(‘auth’, [’$state’, function ($state) {
var auth = {
setToken: function (token) {
window.localStorage[API.token_name] = token;
},
getToken: function () {
return window.localStorage[API.token_name];
},

    getUserData: function () {
        console.log(auth);
        try{
            var token = window.localStorage[API.token_name];
            if (token === '') return;

            var base64Url = token.split('.')[1];
            var base64 = base64Url.replace('-', '+').replace('_', '/');
            console.log()
            return JSON.parse(window.atob(base64)).data;                
        } catch(err) {
            $state.go('signup');
        }
    },
    logout: function () {
        window.localStorage[API.token_name] = '';
        $state.go('signup');
    },
    hasToken: function () {
        return (window.localStorage[API.token_name] !== '');
    },
    redirectIfNotExists: function () {
        if (!auth.hasToken()) {
            $state.go('signup');
        }
    }
};
     
return auth;

}])

.service(‘loader’, function () {
this.show = function (show) {
document.querySelector("#loader").style.display = show ? ‘block’ : ‘none’;
};
})

.service(‘restApi’, [’$http’, ‘loader’, ‘auth’, function ($http, loader, auth) {
this.call = function (config) {

    var headers = {};
    headers[API.token_name] = auth.getToken();


    loader.show(true);
      
    var http_config = {
        method: config.method,
        url: API.base_url + config.url,

        data: typeof (config.data) === 'undefined' ? null : config.data,
        headers: headers
    };


    $http(http_config).then(function successCallback(response) {
        loader.show(false);
        config.response(response.data);

    }, function errorCallback(response) {
        loader.show(false);

        switch (response.status) {
            case 401: // No autorizado
                auth.logout();
                break;
            case 422: // Validación
                config.validationError(response.data);
                break;
            default:
                config.error(response);
                console.log(response.statusText);
                break;
        }
    });
};

}])


#6

Please edit your post, it is not very readable at the moment.
Use the </> button above the input field to format your code, command line output or error message (select the text first, then click the button or wrap it in ``` manually). Check the preview if it looks better. This will make sure your text is readable and if it recognizes the programming language it also automatically adds code syntax highlighting. Thanks.


#7

funciona bien si le quito la etiqueta “ion-content”

este es mi codigo de mi services

angular.module('app.services', [])

.factory('auth', ['$state', function ($state) {
    var auth = {
        setToken: function (token) {
            localStorage[API.token_name] = token;
        },
        getToken: function () {
            return localStorage[API.token_name];
        },

        getUserData: function () {
            console.log(auth);
            try{
                var token = localStorage[API.token_name];
                if (token === '') return;

                var base64Url = token.split('.')[1];
                var base64 = base64Url.replace('-', '+').replace('_', '/');
                console.log()
                return JSON.parse(window.atob(base64)).data;                
            } catch(err) {
                $state.go('signup');
            }
        },
        logout: function () {
            localStorage[API.token_name] = '';
            $state.go('signup');
        },
        hasToken: function () {
            return (localStorage[API.token_name] !== '');
        },
        redirectIfNotExists: function () {
            if (!auth.hasToken()) {
                $state.go('signup');
            }
        }
    };
         
    return auth;
}])

.service('loader', function () {
    this.show = function (show) {
        document.querySelector("#loader").style.display = show ? 'block' : 'none';
    };
})

.service('restApi', ['$http', 'loader', 'auth', function ($http, loader, auth) {
    this.call = function (config) {
        
        var headers = {};
        headers[API.token_name] = auth.getToken();


        loader.show(true);
          
        var http_config = {
            method: config.method,
            url: API.base_url + config.url,

            data: typeof (config.data) === 'undefined' ? null : config.data,
            headers: headers
        };


        $http(http_config).then(function successCallback(response) {
            loader.show(false);
            config.response(response.data);

        }, function errorCallback(response) {
            loader.show(false);

            switch (response.status) {
                case 401: // No autorizado
                    auth.logout();
                    break;
                case 422: // Validación
                    config.validationError(response.data);
                    break;
                default:
                    config.error(response);
                    console.log(response.statusText);
                    break;
            }
        });
    };
}])