-1
So I have an application which gets data from a server, the login function is in services.js that's within a factory. Everything is working fine with Ionic serve but running on an actual device nothing works, the response is empty, and now the xhr requests are pending in chrome for some reason, my session variable( as well as our data variable) which is required to access the server is turning up as null on mobile, but again with ionic serve the session variable is being set just fine.


Can't post any code really, but does anyone have any ideas, I would really appreciate it, I've been troubleshooting this for a few days now and nothing has come up.



    "use strict";

    const VERSION  = "3.0" // 2.43
    const BASELINK = "LINK"+VERSION
    const session = localStorage.getItem('session'); // this gets session id
    const MAINLINK = BASELINK + "&job=Search&session="+session;


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

    .factory('userData', ($http) => {




    //
    //  This is the main function for user data
    //      used by loginCtrl
    //

    const TOKEN = '';
    const POST_CONFIG   = {
        headers: {
            'Content-Type': 'application/x-www-form-urlencoded',

          //  'Content-Type': 'application/x-www-form-urlencoded',
            // application/x-www-form-urlencoded application/json
        }


    }

    var loggedIn = false;

    return {

        login: (credentials) => {
          // Get the username / password entered
        //  $scope.user = document.getElementById('username').value;
        //let username = document.getElementById('username').value;
        //return username;
        console.log(credentials.user)
        console.log("MAINLINK", MAINLINK)
    //=========================---------------------------------------------------------------------------------------------


            // Send a post request to the MobileLogin function
            let body = { "username": credentials.user, "password": credentials.pswd};

            let url = BASELINK+"&job=MobileLogin";
          //  console.log(body, url);
            $http.post(url, body, POST_CONFIG)
                .success(function(data,status,headers,config) {
                    // Successfully contacted the server, now deal with the response
                    let status_code = data.split("|||||")[0];
                    let message = data.split("|||||")[1];
                    console.log(message)
                    console.log("BODY:", POST_CONFIG, body)
                    console.log(body.username)
                    var session='';
                    var username='';
                    eval(data.split('|||||')[2])
                    localStorage.setItem('session',session);
                    console.log('SESSION:',session)
                    localStorage.setItem('username',username);
                    console.log(MAINLINK); // session id
                    if (status_code == 'error') {
                        // There was an error of some sort, most likely entered incorrect information
                        document.getElementById('loginmessage').textContent = message;
                    } else {
                        // Login was successful, now redirect to the Home page
                        document.getElementById('loginmessage').textContent = 'Login Successful!';
                    }
                })
                .error(function(data,status,headers,config) {
                    // There was an error contacting the server
                    document.getElementById('loginmessage').textContent = 'Cannot connect to server.';
                    console.log("DATA: "+data);
                    console.log("STATUS: "+status);
                    console.log("HEADERS: "+headers);
                    console.log("CONFIG: "+config);

                });
    //=========================---------------------------------------------------------------------------------------------
            console.log(credentials);
            loggedIn = true;
        },
        isLoggedIn: () => {
            return loggedIn;



        }
    };
})
.factory('searchData', () => {
  var storedproductCode= "";
  var storedProductLine= "";


     return {
         getValue: function(viewId) {
             return storedProductLine[viewId];
         },
         setValue: function(newValue,viewId) {
             storedProductLine[viewId] = newValue
         },
         deleteOrder: function(viewId) {
             delete storedProductLine[viewId];
         }
     };


})

I've added my services.js code above, do you need my controller code as well? The basic idea is when im trying to access a page on mobile the session id is coming back as null, data is coming back as null as well, this is not a problem with ionic serve only when running on mobile, we just went and fixed any cors issues we were having so that shouldnt be it

Darren
  • 1
  • 2

2 Answers2

0

did u try ionic lab? why dont u post your service.ts code? it could be useful to better understand where the problem is. it maybe just a matter of request's headers

Nik Rubblers
  • 142
  • 1
  • 13
  • You mean services.js? – Darren Aug 21 '18 at 13:40
  • Also what does ionic serve --lab do over the regular ionic serve? – Darren Aug 21 '18 at 13:50
  • We've had a cors issue before, could that still possibly be causing issues? We've added things to allow for cors on the server but it still doesn't seem to work without a cors plugin on chrome at least. Our headers 'Content-Type': 'application/x-www-form-urlencoded', 'Crossdomain': 'true', "Access-Control-Allow-Methods": "GET, POST, DELETE, PUT, OPTIONS", "Access-Control-Allow-Headers": "X-Requested-With, Content-Type, Authorization, Origin, Accept", 'Access-Control-Allow-Origin': '*', – Darren Aug 21 '18 at 14:57
  • Hi i've added my code for services.js let me know if you need more like my controller – Darren Aug 21 '18 at 17:03
  • I think u need to play a little more with the headers of the request – Nik Rubblers Aug 22 '18 at 14:30
  • Our cors issues have been fixed, however we still can't get session to set properly on mobile, could it be something related to localStorage? – Darren Aug 22 '18 at 14:53
  • I managed to fix the session id not being stored(it proceeded to work once on mobile but now it doesnt work anymore), now however i'm getting data is null still, and networks requests are pending and then fail after about 2 minutes – Darren Aug 22 '18 at 16:46
  • Any ideas? Could use some help – Darren Aug 22 '18 at 21:34
0

Fixed! The issue was relating to our internal network. Please close

Darren
  • 1
  • 2