I got stuck on working with my server and don't find a solution for now. I hope you can help me.
I have a service from where I want to send a POST request. The service looks like: (I want to use a service, because I need to call this from different sites)
angular.module('schwimmfestivalApp')
.service('groupService', function($http) {
// AngularJS will instantiate a singleton by calling "new" on this function
var url = '../../../server/func/getGroups.php';
var createGroupUrl = '../../../server/func/setGroup.php';
var groupCategoryUrl = '../../../server/func/getGroupCategory.php';
this.getGroups = function() {
return $http.get(url, {cache: false}).then(response => {
console.log('Groups: ' + JSON.stringify(response.data));
return response.data;
});
};
this.createGroup = function(group) {
return $http.post(createGroupUrl, {'group': group}, {headers: {'Content-Type': 'application/x-www-form-urlencoded'}}).then(response => {
console.log('Group to Create: ' + group);
console.log('Resonse: ' + JSON.stringify(response));
console.log('Create Group: ' + response.data);
return response.data;
});
};
});
The getGroups() Method is working fine.
My Server code (not written by me) for the setGroups() Method looks like:
<?php
require_once '../connect/connect_db.inc';
require_once '../class/gruppe.php';
require_once '../func/getHeader.php';
$db = getConnection();
getHeader('json');
if(isset($_POST['group'])){
$idGruppe = 0;
//$gruppeJson = json_decode(file_get_contents("php://input"));
$gruppeJson = json_decode($_POST['group']);
$sql = "INSERT INTO gruppe ";
$sql .="( name, gruppe_typ_id) VALUES ('";
$sql .= $gruppeJson->{'name'} . "', '";
$sql .= $gruppeJson->{'idKategorie'};
$sql .=" ')";
if ( $db -> query($sql)) {
$idGruppe = $db -> insert_id ;
$selectGroup = "SELECT gruppe.id, gruppe.name, gruppe.gruppe_typ_id AS idKategorie, gruppe_typ.name AS kategorie ";
$selectGroup .= "FROM gruppe, gruppe_typ ";
$selectGroup .= "WHERE gruppe.gruppe_typ_id = gruppe_typ.id AND gruppe.id = " . $idGruppe;
if ($result = $db -> query($selectGroup)) {
while($row = $result->fetch_assoc()) {
$gruppe = new gruppe();
$gruppe->id = $row['id'];
$gruppe->name = $row['name'];
$gruppe->idKategorie = $row['idKategorie'];
$gruppe->kategorieName = $row['kategorie'];
}
echo json_encode($gruppe);
} else {
echo $db->error;
echo "</br> " . $selectGroup;
}
} else{
}
}
I have read something about you have to read the data with file_get_contents. But I don't get there. The if(isset($_POST['group']))
doesn't validate to true.
So how do I have to build my Request to get into this if ? Do you have any suggestions for me?
Thanks in advance Dominic