0

This is my ionic code

dialogflow(question) {
    let headers = new Headers();
    headers.append('Content-Type','application/json');
    return this.http.post('http://localhost:3000/api/dialogflow',question,{headers: headers})
    .map(res => res.json());
  }

In the console log I'm getting errors as

POST http://localhost:3000/api/dialogflow 400 (Bad Request) This is from routes files(back-end)

router.post('/dialogflow', function(req,res) {
  console.log('reached here');

In the back-end(mean) console it is throwing an error as

SyntaxError: Unexpected token h in JSON at position 0
    at JSON.parse (<anonymous>)
    at createStrictSyntaxError (D:\product\project-1\node_modules\body-parser\lib\types\json.js:158:10)
    at parse (D:\product\project-1\node_modules\body-parser\lib\types\json.js:83:15)
    at D:\product\project-1\node_modules\body-parser\lib\read.js:121:18
    at invokeCallback (D:\product\project-1\node_modules\raw-body\index.js:224:16)
    at done (D:\product\project-1\node_modules\raw-body\index.js:213:7)
    at IncomingMessage.onEnd (D:\product\project-1\node_modules\raw-body\index.js:273:7)
    at emitNone (events.js:106:13)
    at IncomingMessage.emit (events.js:208:7)
    at endReadableNT (_stream_readable.js:1064:12)
    at _combinedTickCallback (internal/process/next_tick.js:138:11)
    at process._tickCallback (internal/process/next_tick.js:180:9)

How can I resolve this?

2
  • How does your question object look like? Commented Oct 5, 2018 at 18:53
  • it's not an object it's just a string. Now I have converted it in to the object it's working fine. Commented Oct 6, 2018 at 10:44

1 Answer 1

1

The object being sent is not correctly serialized.

at JSON.parse (<anonymous>)
at createStrictSyntaxError (D:\product\project-1\node_modules\body-parser\lib\types\json.js:158:10)
at parse (D:\product\project-1\node_modules\body-parser\lib\types\json.js:83:15) 
Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.