Express正文解析器:位置0的JSON中的意外令牌# [英] Express body-parser: Unexpected token # in JSON at position 0

查看:168
本文介绍了Express正文解析器:位置0的JSON中的意外令牌#的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试从Polymer的登录名返回用户数据.我可以在Postman上使用它,但是在将其转换为Polymer时遇到了麻烦.

I am trying to return user data from a login with Polymer. I have it working with Postman, but am having trouble translating it into Polymer.

在Postman中,此操作返回一个JSON对象,但在Polymer中,它返回的是未定义的对象.

In Postman this returns a JSON object, but in Polymer it is returning undefined.

聚合物客户端代码[连接到node.js服务器]

Polymer Client Code [Connecting to node.js server]

<iron-ajax id="ajaxUser"
  url="http://localhost:8080/login"
  method="post"
  handle-as="json"
  content-type="application/json"
  headers='{"Access-Control-Allow-Origin": "*"}'
  params="[[params]]"
  on-response="saveUserCredentials"
  last-response="{{user}}"></iron-ajax>

...

<paper-input id="username"></paper-input>
<paper-input id="password"></paper-input>
<paper-button on-tap="loginUser"></paper-button>

...

loginUser() {
  this.params = {"username": this.$.username.value, "password": this.$.password.value};
  console.log(this.params); // logs this.params as populated JSON 
  let request = this.$.ajaxUser.generateRequest();
  request.completes.then(req => {
    console.log(req);       // no log
    console.log(this.user); // no log
  })
}

saveUserCredentials() {
  console.log(this.user);
}

服务器

// Middleware
app.options('*', cors(corsOptions)) // preflight OPTIONS; put before other routes
app.use(formData.parse(formBody))   // parse req.body data from `express-form-data` module
app.use(bodyParser.json())
app.use(apiLimit)                   // Rate limit applied to all requests, can apply to specific endpoints
app.use((req, res, next) => {       // Enable Cross-Origin Resource Sharing (CORS)
  res.header("Access-Control-Allow-Origin", "*")
  res.header("Access-Control-Allow-Methods", "GET,HEAD,OPTIONS,POST,PUT")
  res.header("Access-Control-Allow-Headers", "Origin, X-Requested-With, content-type, Accept, Authorization, x-api-key")
  next()
}) 

错误

SyntaxError:JSON中位置0处的意外令牌#
在JSON.parse()
在createStrictSyntaxError(C:\ node_modules \ body-parser \ lib \ types \ json.js:157:10)下
解析时(C:\ node_modules \ body-parser \ lib \ types \ json.js:83:15)
在C:\ node_modules \ body-parser \ lib \ read.js:121:18
在invokeCallback(C:\ node_modules \ raw-body \ index.js:224:16)
完成时(C:\ node_modules \ raw-body \ index.js:213:7)
在IncomingMessage.onEnd(C:\ node_modules \ raw-body \ index.js:273:7)
在IncomingMessage.emit(events.js:159:13)
在endReadableNT(_stream_visible.js:1062:12)
在process._tickCallback(internal/process/next_tick.js:152:19)

SyntaxError: Unexpected token # in JSON at position 0
at JSON.parse ()
at createStrictSyntaxError (C:\node_modules\body-parser\lib\types\json.js:157:10)
at parse (C:\node_modules\body-parser\lib\types\json.js:83:15)
at C:\node_modules\body-parser\lib\read.js:121:18
at invokeCallback (C:\node_modules\raw-body\index.js:224:16)
at done (C:\node_modules\raw-body\index.js:213:7)
at IncomingMessage.onEnd (C:\node_modules\raw-body\index.js:273:7)
at IncomingMessage.emit (events.js:159:13)
at endReadableNT (_stream_readable.js:1062:12)
at process._tickCallback (internal/process/next_tick.js:152:19)

推荐答案

问题是您的POST主体为空(如请求有效负载的屏幕截图所示).您正在尝试通过<iron-ajax>.params传递用户凭证,该凭证旨在增加URL查询参数(请注意请求URL如何包含用户凭证作为参数).

The problem is your POST body is empty (as seen in the screenshot of your request payload). You're attempting to pass the user credentials via <iron-ajax>.params, which is intended to augment the URL query parameters (notice how the request URL contains the user credentials as parameters).

要设置POST主体,请设置

To set the POST body, set <iron-ajax>.body (and change this.params to this.body) instead:

<iron-ajax body="[[body]]" ...>

演示

这篇关于Express正文解析器:位置0的JSON中的意外令牌#的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆