简体   繁体   中英

how can i pass json array from php file on a server to my ionic app?

i'm developing an Ionic app and i must pass data from backend (php file on a server) to ionic app and from ionic app to backend. I tried this:

.controller('AppCtrl',function($scope,$ionicPlatform,$location,$http,$ionicHisto
ry, $ionicModal, $timeout,$cordovaSQLite) {


$ionicPlatform.ready(function(){


$http.get('http://http://localhost/ShuttleFIX/json.php')
    .success(function(data,status,headers,config){
          var user = data;
          for(i = 0; i<user.length; i++){
            var cell = user[i].cell;
            var nome = user[i].nome;
            var cognome = user[i].cognome;
            var mail = user[i].mail;
            var codF = user[i].codF;
            var pwd = user[i].pwd;
          }
    }
});

I know i must use http request but i don't know how, can someone help me? Thank's

The first thing to determine is how you return and receive data from your server php. Define the method GET or POST , if GET you must assemble the URL with the parameters you want to send. If Post, they are usually shipped with JSON format and pass in the parameter data

It seems not beam tried a lot but is more or less like this:

GET :

.controller('AppCtrl', function($scope, $http){
    var url = "http://myhost/name="+$scope.name;
    $http({ method: 'GET', url: url, dataType: "json", contentType: "application/x-www-form-urlencoded" })
    .success(function(data) {
        console.log(data);
    })
    .error(function(response) {
        console.log("error");
    })
    .finally(function() {

    });
})

POST:

    var data = {
         name : $scope.name
    }
    $http({ method: 'POST', url: url, dataType: "json", data: data})
    .success(function(data) {

    })
    .error(function(response) {
          console.log("error");
    })
    .finally(function() {

    });

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