HTTP POST response failure angular 16

91 Views Asked by At

I'm bothered with an http failure response "Unknown error" and I didn't found where is the problem. With PDO when I send my object to Mysql server login with messageText "string" all is good and connected to Mysql, but when I add a query POST to table there is this error:

{"headers": {"normalizedNames": {},"lazyUpdate": null,"headers": {}},"status": 0,"statusText": "Unknown     Error","url": "https://www.boulangerieqc.com/assets/crudmysql/insert_message.php","ok": false,"name":     "HttpErrorResponse","message": "Http failure response for     https://www.boulangerieqc.com/assets/crudmysql/insert_message.php: 0 Unknown Error","error":     {"isTrusted": true}}

my .ts to catch my messageText is:

let connectionMysqlObjMessage={messageText:this.newMessage.value.messageText,db_host:this.extract.boulanger.db_host,db_username:this.extract.boulanger.db_username,db_name:this.extract.boulanger.db_name,db_password:this.extract.boulanger.db_password,}this.api.createMessageMysql(connectionMysqlObjMessage).subscribe((res)=>{console.log('avant api='+res)

my api:

createMessageMysql(MessageObj: Object) {return this.http.post(this.baseUrl + 'insert_message.php',MessageObj, {responseType: 'json'})}

If you have some ideas I will be happy to solve this.

3

There are 3 best solutions below

0
Oscar On BEST ANSWER

Seems It's a CORS issue. Try to disable (or configure) it on server side. This should solved your issue

1
ferhado On

Are you using interceptors in your Angular application? They might be sending OPTIONS requests, which could lead to HTTP status 0 issues. To address this, first, ensure your PHP script handles OPTIONS requests:

if ($_SERVER['REQUEST_METHOD'] === 'OPTIONS') {
    http_response_code(200);
    exit;
}

Additionally, don't forget to include OPTIONS in the allowed methods in your CORS settings:

header('Access-Control-Allow-Methods: GET, POST, OPTIONS, PUT, DELETE');

This modification in your server code ensures proper handling of OPTIONS requests and CORS compliance, potentially resolving the status 0 issue.

3
jf Levis On

No CORS, header was good. But I found that I can't send an OBJECT with httpClient.post.

All is good when I cast Object to JSON before:

addMessage(message: Message) {
  let messageJSON=JSON.stringify(message)
  return this.http.post(this.baseUrl + 'insert_message.php', messageJSON, {responseType: 'text'})