1

My app.component.ts contains

login() {
    var user = `{ email: ${this.email}, password: ${this.password} }`
    const headers = new HttpHeaders()
                    .set('Authorization', 'my-auth-token')
                    .set('Content-Type', 'application/json');
    this.http.post('http://localhost:3000/signin', user, {
          headers: headers 
    }).subscribe(data => {

   });
   console.log(`email: ${this.email} password: ${this.password}`) 
}

When trying to get the data in node I get

error: SyntaxError: Unexpected token e in JSON at position 2

I am using

req.body

to get the data. What is the correct way to parse the JSON data? Also wanted to know if this is the correct way to pass form data from angular to node?

6
  • You should stringify the user like this : JSON.stringify(user) Commented May 18, 2018 at 7:45
  • That gives error: SyntaxError: Unexpected token " in JSON at position 0 Commented May 18, 2018 at 7:52
  • have you print user in console.log what you get there Commented May 18, 2018 at 7:56
  • Yes. I get it correct. Commented May 18, 2018 at 7:59
  • @TechTeam You use Angular 6 HttpClient ? Commented May 18, 2018 at 8:01

4 Answers 4

2
var user = { 
    email: this.email, 
    password: this.password 
}
...
this.http.post('http://localhost:3000/signin',user).subscribe(...)
Sign up to request clarification or add additional context in comments.

2 Comments

why should i remove the headers ?
You should change the "user" to be Object type rather than String type
1

With Angular 6 HttpClient, you don't need to stringify your object (see POST example from official doc). In the same way, for a GET request, HttpClient parse json data for you.

Try this :

login() {
   const user = { email: this.email, password: this.password };

   const headers = new HttpHeaders({
      'Authorization': 'my-auth-token',
      'Content-Type': 'application/json'
   });

   this.http.post('http://localhost:3000/signin', user, {
     headers: headers 
   }).subscribe(data => {});
 console.log(`email: ${this.email} password: ${this.password}`) }

Comments

1
user= {
   email: [email protected],
   password: 123,
    }               
            $http({
                method: "POST",
                url: http://localhost:3000/signin,
                data: JSON.stringify(user),
                headers: headers 
            }).success(function(data) {
               // you can print data hera
                }
            }).error(function() {

            });

2 Comments

this has to be added in app.component.html file ?
you can also use the RXJS library , to hit the API, using RXJS library is the better way
0

First i thought you using an object for the user, than a saw it's a string. Your data is not with correct json format. Try to put quotes on the fields name

var user = `{ "email": ${this.email}, "password": ${this.password} }`

WRONG!

You should stringify the data - JSON.stringify(user)

this.http.post('http://localhost:3000/signin', JSON.stringify(user), {
  headers: headers 
}).subscribe(data => {

});

4 Comments

that gives error: SyntaxError: Unexpected token " in JSON at position 0
also doing this makes it to a string whereas i have mentioned in the header to use content type json
Yeah, I saw I was wrong, I edit my answer. This should work.
Okay. But i still get error: SyntaxError: Unexpected token " in JSON at position 0. I should be fetching it using request.body , right ?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.