Express body-parser:位置 0 JSON 中的意外标记 #
Express body-parser: Unexpected token # in JSON at position 0
我正在尝试 return 来自 Polymer 登录的用户数据。我在 Postman 中使用它,但在将它翻译成 Polymer 时遇到了问题。
在 Postman 中,这个 return 是一个 JSON 对象,但在 Polymer 中,它是 return 未定义的。
Polymer 客户端代码[连接到 node.js 服务器]
<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: 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 如何包含用户凭据作为参数)。
要设置 POST
正文,请改为设置 <iron-ajax>.body
(并将 this.params
更改为 this.body
):
<iron-ajax body="[[body]]" ...>
我正在尝试 return 来自 Polymer 登录的用户数据。我在 Postman 中使用它,但在将它翻译成 Polymer 时遇到了问题。
在 Postman 中,这个 return 是一个 JSON 对象,但在 Polymer 中,它是 return 未定义的。
Polymer 客户端代码[连接到 node.js 服务器]
<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: 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 如何包含用户凭据作为参数)。
要设置 POST
正文,请改为设置 <iron-ajax>.body
(并将 this.params
更改为 this.body
):
<iron-ajax body="[[body]]" ...>