简体   繁体   中英

Ionic and angular - Sending http requests to server and receiving JSON data back, this works with Ionic serve but not with Ionic Cordova run Android

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

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

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM