简体   繁体   English

SyntaxError:JSON中位置0处的意外令牌#

[英]SyntaxError: Unexpected token # in JSON at position 0

I have a form that send a search term to node. 我有一个将搜索项发送到节点的表格。 In the request header I do set the content type to json, but I still get this error. 在请求标头中,我确实将内容类型设置为json,但仍然收到此错误。 I have no idea what I'm doing wrong. 我不知道我在做什么错。 new to node js. 节点js的新功能。 I am using angular 4 as the front end: 我正在使用angular 4作为前端:

This is my component code for the search form: 这是我用于搜索表单的组件代码:

  onSearch(){
console.log('the sent term is: ', this.searchForm.value.term)
this._auth.searchTweets(this.searchForm.value.term)
  .subscribe(
    (res) => {
      console.log(res)
    }
  )
 }

This is my service code which sends the term to the node backend: 这是我的服务代码,它将术语发送到节点后端:

  searchTweets(term){
console.log('from auth search: ', term)
let headers = new Headers();
headers.append('Content-Type', 'application/json');
return this.http.post('http://localhost:4000/users/profile',term, {headers:headers})
  .map(res => res.json());
 }

and my code for the node side is: 我在节点端的代码是:

router.post('/profile',(req,res,next) => {
console.log('the backend term: ',req.body.term)
})

In the network tab of dev tools i get html and not json? 在开发工具的网络标签中,我得到的是html而不是json? what am i missing here? 我在这里想念什么?

what i try to log the res in the node side : 我尝试在节点端记录res的内容:

SyntaxError: Unexpected token # in JSON at position 0
    at Object.parse (native)
    at createStrictSyntaxError (C:\xampp\htdocs\angssrtest\node_modules\body-parser\lib\types\json.js:157:10)
    at parse (C:\xampp\htdocs\angssrtest\node_modules\body-parser\lib\types\json.js:83:15)
    at C:\xampp\htdocs\angssrtest\node_modules\body-parser\lib\read.js:121:18
    at invokeCallback (C:\xampp\htdocs\angssrtest\node_modules\raw-body\index.js:224:16)
    at done (C:\xampp\htdocs\angssrtest\node_modules\raw-body\index.js:213:7)
    at IncomingMessage.onEnd (C:\xampp\htdocs\angssrtest\node_modules\raw-body\index.js:273:7)
    at ZoneDelegate.invokeTask (C:\xampp\htdocs\angssrtest\node_modules\zone.js\dist\zone-node.js:425:31)
    at Zone.runTask (C:\xampp\htdocs\angssrtest\node_modules\zone.js\dist\zone-node.js:192:47)
    at ZoneTask.invokeTask (C:\xampp\htdocs\angssrtest\node_modules\zone.js\dist\zone-node.js:499:34)
    at IncomingMessage.ZoneTask.invoke (C:\xampp\htdocs\angssrtest\node_modules\zone.js\dist\zone-node.js:488:48)
    at emitNone (events.js:86:13)
    at IncomingMessage.emit (events.js:185:7)
    at endReadableNT (_stream_readable.js:974:12)
    at ZoneDelegate.invokeTask (C:\xampp\htdocs\angssrtest\node_modules\zone.js\dist\zone-node.js:425:31)
    at Zone.runTask (C:\xampp\htdocs\angssrtest\node_modules\zone.js\dist\zone-node.js:192:47)

and by the way i have a get on this same route that works. 顺便说一句,我有一条可行的方法。 so i don't get why in the post one i have this issue 所以我不明白为什么在后一篇中我有这个问题

The error is because you are receiving the html file, res.json() is not able to parse the HTML file. 该错误是因为您正在接收html文件,res.json()无法解析HTML文件。 Make sure the appropriate route exists in node and you are sending the proper response. 确保节点中存在适当的路由,并且您正在发送正确的响应。

I notice the endpoint you are hitting is http://localhost:4000/users/profile and the one you are listening is '/profile'. 我注意到您命中的端点是http:// localhost:4000 / users / profile ,而您正在监听的端点是'/ profile'。 users is missing in route. 用户在路线中丢失。 And i assume you have a /* listener which returns a html file and thats what you get as a response. 我假设您有一个/ *侦听器,该侦听器返回一个html文件,这就是您作为响应得到的内容。

searchTweets(term) {
  console.log('from auth search: ', term)
  let headers = new Headers();
  headers.append('Content-Type', 'application/json');
  return this.http.post('http://localhost:4000/users/profile',term, 
  {headers:headers})
    .map(res => res.json());
 }

And the route listener doesnt send any response in return. 并且路由侦听器不发送任何响应作为回报。

router.post('/profile',(req,res,next) => {
 console.log('the backend term: ',req.body.term)
})

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM