我不能从我的ionic应用程序发送值到我的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的值,但我做不到。当我看到控制台只有"undefined"错误显示。
<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
。
设置好了吗?Ionic会发送json格式,但是PHP默认不能接受json。你需要设置php接收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 ?
});};