Hello,
I’m trying to POST and GET data from a remote MySQL database with PHP from Ionic -v: 6.13.1 request with using of import { HttpClient } from '@angular/common/http';
as private http: HttpClient
in constructor
::
request.page.ts:
post() {
let myHeaders = { 'Content-Type': 'application/json' };
let postParams = '&ID=' + 1;
this.http
.post(this.phpPath + 'group.php', postParams, { headers: myHeaders })
.subscribe(
(data) => {
console.log(data);
},
(error) => {
console.log('Error' + error);
}
);
}
connect.php:
<?php
header("Access-Control-Allow-Origin: *");
header('Content-type: text/xhtml; charset=windows-1255');
$con = new mysqli('serverIp', 'username', 'password');
$con->set_charset('utf8');
if (!$con)
{
echo 'Not Connected To Server';
}
if (!mysqli_select_db ($con, 'museit_db'))
{
echo 'Database Not Selected';
}
?>
I’ve tried add connect.php:
header('Access-Control-Allow-Origin: *');
header("Access-Control-Allow-Headers: Origin, X-Requested-With, Content-Type, Accept, Access-Control-Request-Method, Authorization");
header('Access-Control-Allow-Methods: POST, GET, DELETE, PUT, PATCH, OPTIONS');
and group.php:
<?php
require 'connect.php';
$ID = mysqli_real_escape_string($con, $_POST["ID"]);
$sql = "SELECT ID, keys, folders FROM document ORDER BY ID";
$result = mysqli_query($con, $sql);
$myArray = array();
if (mysqli_num_rows($result) > 0) {
while($row = mysqli_fetch_assoc($result)) {
$myArray[] = $row;
}
echo json_encode($myArray);
} else {
echo "0 results";
}
mysqli_close($con);
?>
but chrome console:
Access to XMLHttpRequest at ‘path/group.php’ from origin ‘http://localhost:8100’ has been blocked by CORS policy: Request header field content-type is not allowed by Access-Control-Allow-Headers in preflight response.
1 post - 1 participant
Read full topic