I'm learning how to use HttpClient.post to update MySQL database. The table i'm inserting is:
|id - log|
The PHP file is as follows,
<?php
header('Access-Control-Allow-Origin: *');
header('Access-Control-Allow-Headers: X-Requested-With');
header('Access-Control-Allow-Methods: POST, GET, OPTIONS');
include 'db.php';
$postdata=file_get_contents("php://input");
$request = json_decode($postdata);
$message = $request->message;
// Create connection
$conn = new mysqli($server_name, $mysql_username, $mysql_password, $db_name);
$sql = "INSERT INTO shootingchatlog (id, log) VALUES (NULL, '$message')";
if($conn -> query($sql) === TRUE ){
echo "message Uploaded Successfully";
}else{
echo "Error Uploading Image";
}
mysqli_close($conn);
I keep receiving errors at the line
$message = $request->message;
which brings me to think that the problem has to do with my code here in angular.
httpOptions = {
headers: new HttpHeaders({
'Content-Type': 'application/json'
})
};
constructor(private http: HttpClient) { }
public uploadService(mess: string): void {
console.log(mess);
this.http.post(this.url, JSON.stringify({"messasge": mess}), this.httpOptions)
.subscribe((res: Response) => {
console.log(res);
},
err => {
console.log("Error occured", err);
});
}
The most common error I get is:
SyntaxError: Unexpected token < in JSON at position 0 at JSON.parse
I'm really not sure what I'm doing incorrectly. There doesn't seem to be any documentation on this. Or if it is, it's usually too abstract. Any help would be grateful!