hi there.
I am new to ionic. I am working on getting the result on basic ionic http post to php. i dont know where i did wrong but php seems to be not receive any data, in this case a string. In php, I just want to know if the variable got the string but every result I get is the variable is null or didn’t get the data. I did make some tutorials but didn’t success. below is my code
index.html
{{message}}
app.js
.controller(‘postData’, function($scope, $http, $templateCache) {
$scope.add = function () {
var request = $http({
method: “POST”,
url: “http://192.168.1.124/upload.php”,
data: {
datastr:$scope.itemInput
//pass:$scope.item2
},
headers: { ‘Content-Type’: ‘application/x-www-form-urlencoded; charset=UTF-8;’ },
cache: $templateCache
});
/* Check whether the HTTP Request is Successfull or not. */
request.success(function (data) {
$scope.message = "From PHP file : " + data;
}).
error(function(err) {
$scope.message = “error”;
});
};
});
upload.php
if (isset($_SERVER[‘HTTP_ORIGIN’])) {
header(“Access-Control-Allow-Origin: {$_SERVER[‘HTTP_ORIGIN’]}”);
header(‘Access-Control-Allow-Credentials: true’);
header(‘Access-Control-Max-Age: 86400’); // cache for 1 day
}
if ($_SERVER[‘REQUEST_METHOD’] == ‘OPTIONS’) {
if (isset($_SERVER['HTTP_ACCESS_CONTROL_REQUEST_METHOD']))
header("Access-Control-Allow-Methods: GET, POST, OPTIONS");
if (isset($_SERVER['HTTP_ACCESS_CONTROL_REQUEST_HEADERS']))
header("Access-Control-Allow-Headers: {$_SERVER['HTTP_ACCESS_CONTROL_REQUEST_HEADERS']}");
exit(0);
}
$data = file_get_contents(“php://input”);
if (isset($data)) {
$request = json_decode($data);
$datastring = $request->datastr;
if ($datastring != "") {
echo "Server returns: " . $datastring;
}
else {
echo "data empty!";
}
}
else {
echo "error!";
}