Error making an Angular POST request (Syntaxerror: Unexpected token s in JSON at position 0 at JSON.parse)

Asked

Viewed 990 times

1

Angular:

public urlConvenio: string = "http://localhost/teste/sistema/backend/controller/convenio/listar.php";

const httpOptions = {
   headers: new HttpHeaders({
     'Content-Type': 'application/json'
   })
};

//REQUISIÇÃO
public enviaDados() {
    this.http.post(this.urlPost, 
                   JSON.stringify(this.myForm.value), 
                   this.httpOptions).subscribe(res => {
       console.log(res);
    })
 }

Error:

msg de erro

Server:

codigo php

  • I think you don’t need the stringify, the angular already made it for you as a pattern.

1 answer

1


Add the header responseType in his constant httpOptions

 const httpOptions = {
        headers: new HttpHeaders({ 'Content-Type': 'application/json', 'responseType': 'text' })
    }
  • that’s right, thanks friend!

Browser other questions tagged

You are not signed in. Login or sign up in order to post.