简体   繁体   English

如何将json数组从服务器上的php文件传递到我的Ionic应用程序?

[英]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. 我正在开发一个Ionic应用程序,我必须将数据从后端(服务器上的php文件)传递到Ionic应用程序,并从Ionic应用程序传递到后端。 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? 我知道我必须使用http请求,但我不知道如何,有人可以帮助我吗? Thank's 谢谢

The first thing to determine is how you return and receive data from your server php. 确定的第一件事是如何从服务器php返回和接收数据。 Define the method GET or POST , if GET you must assemble the URL with the parameters you want to send. 定义方法GETPOST ,如果使用GET,则必须将URL与要发送的参数组合在一起。 If Post, they are usually shipped with JSON format and pass in the parameter data 如果是Post,则通常以JSON格式提供,并传递参数data

It seems not beam tried a lot but is more or less like this: 似乎束没有尝试很多,但或多或​​少是这样的:

GET : 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() {

    });

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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