如何在ng-repeat中发送输入值?离子的

时间:2016-10-18 13:55:32

标签: php angularjs post ionic-framework

我无法从我的离子应用程序向我的php服务器发送值。我在html中使用这些代码;

  <form ng-submit="addfavourite()">
      <input type="text" ng-model="veri.mac_id">
      <input type="submit" value="" class="favourite">
  </form>

我尝试在控制器中发布一些值;

$scope.veri = {};

$scope.addfavourite = function(){

    var links = 'http://www.example.com/api.php';
    $scope.veri.user_id = loggeduser;
        $http.post(links, {user_id : $scope.veri.user_id, mac_id : $scope.veri.mac_id}).then(function (fav){
        $scope.response = fav.data;
        if ($scope.response == 1) {
          $scope.messages = 'Correct.';
        }
        else if ($scope.response == 0)
        {
          $scope.messages = 'False.';
        }
        console.log($scope.veriler.mac_id);
    });
};

我尝试了一些东西来获得mac_id的价值,但我无法做到。当我只看到控制台&#34; undefined&#34;错误显示。

2 个答案:

答案 0 :(得分:0)

  <form>
      <input type="text" ng-model="veri.mac_id">
      <input type="submit" value="SEND" class="favourite" ng-click='addfavourite()'>
  </form>

JS

$scope.veri = {};
$scope.addfavourite = function(){
alert('DATA TO SEND '+$scope.veri.mac_id);
}

你可以像我下面写的那样修改你的代码。在哪里添加ng-click并删除ng-submit

答案 1 :(得分:0)

你已经设置了这些? Ionic将发送json格式,但PHP无法接受json的默认值。 您需要设置php receive json格式。

header('Content-Type: application/json; charset=UTF-8');

//Allow all domain names to be accessed
header('Access-Control-Allow-Origin:*');

$content_type_args = explode(';', $_SERVER['CONTENT_TYPE']);
if ($content_type_args[0] == 'application/json') {
    $_REQUEST = json_decode(file_get_contents('php://input'), true);
};

试试吗?

$scope.addfavourite = function(){

    var user_id = $scope.veri.user_id;
    var mac_id = $scope.veri.mac_id;

    var data = {};
    data.user_id = user_id;
    data.mac_id = mac_id;

    var links = 'http://www.example.com/api.php';

    $http.post(links, data).then(function (fav) {

        $scope.response = fav.data;

        if ($scope.response == 1) {
            $scope.messages = 'Correct.';
        }
        else if ($scope.response == 0) {
            $scope.messages = 'False.';
        }

        console.log($scope.veriler.mac_id); //$scope.veri.mac_id ?

});};