POST JSON with iron-ajax to SQL Server with Node



我正在尝试从使用Polymer登录时返回用户数据。 我让它与邮递员一起工作,但在将其翻译成聚合物时遇到麻烦。

在Postman中,这返回了一个JSON对象,但在Polymer中,它返回undefined

聚合物客户端代码 [连接到节点.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);       // logs <iron-request></iron-request>
console.log(this.user); // logs []
})
.catch(rejected => {
console.log(rejected.request); // not returned
console.log(rejected.error);   // not returned
})
}
saveUserCredentials() {
console.log(this.user);
}

Node, Express, mssql Server Code [连接到 SQL Server 数据库]

app.post("/login", (req, res) => {
session.login(req, res)
})

exports.login = (req, res) => {
sql.connect(config.properties)
.then(pool => {
pool.request()
.input('user', sql.VarChar(50), req.body.username)
.input('password', sql.VarChar(50), req.body.password)
.query("SELECT role FROM Login WHERE username = @user AND password = @password")
.then(response => res.send(response))
.catch(err => res.send(err))
})
}

错误

语法错误:JSON 中位置 0 处出现意外的标记 #。 at JSON.parse ()
at createStrictSyntaxError (C:ode_modules\body-parser\lib\types\json.js:157:10) at parse (C:ode_modules\body-parser\lib\types\json.js:83:15)
at C:ode_modules\body-parser\lib\read.js:121:18 . at invokeCallback (C:ode_modules\raw-body\index.js:224:16)at done (C:ode_modules\raw-body\index.js:213:7)at IncomingMessage.onEnd (C:ode_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)




第一个问题似乎是您的服务器期望在请求中使用 JSON 对象,但由于请求中缺少Content-Type标头,服务器将请求视为字符串。要设置 JSON 请求的Content-Type,请将<iron-ajax>.contentType设置为application/json

<iron-ajax content-type="application/json" ...>

好的,我能够通过将 content-type=application/json 设置为 iron-ajax 属性来获得服务器响应。我现在在位置 0 的 JSON 中收到意外的令牌 #作为服务器端错误......

这听起来像是请求实际上不是有效的 JSON(因为它包含一个#作为第一个字符)。使用 Chrome DevTools Network Panel 检查有效负载的实际内容。我不会完全依赖您的代码中的console.log

此外,当我提交时,它现在提出两个请求。一个具有内容类型集(解析为 200),另一个具有该内容类型集,解析为 400,并出现分析错误。

第一条消息可能是预检请求,该请求(作为 CORS 的一部分)发送到服务器以检查content-type="application/json"是否为允许的标头。第二条消息是预期的数据请求,但失败并显示以下错误。

客户端错误为No 'Access-Control-Allow-Origin' header is present on the requested resource. Origin 'http://localhost:4001' is therefore not allowed access. The response had HTTP status code 400.

您的服务器需要启用 CORS 请求。有多种方法可以实现此目的,但最简单的 Node 解决方案可能是使用cors包:

const cors = require('cors');
const app = express();
app.use(cors());

最新更新