【发布时间】:2021-09-24 19:43:13
【问题描述】:
我正在创建一个登录表单并尝试获取会话状态的数据。我收到错误“Uncaught (in promise) SyntaxError: Unexpected token
Login.js file
import React, { Component } from 'react';
import { Header } from './Header';
class Login extends Component {
constructor(props) {
super(props);
this.state = { fields: { "idAccount": 0, "idAccountUser": 0, "UserFirstName": "",
"UserLastName": "", "UserEmail": "", "UserCulture": "", "AllowContentDistribution": false },
errors: {} };
this.state = { inputFields: { "userName": "", "password": "" } };
}
authenticateUser(e) {
e.preventDefault();
var userName = e.userName;
var password = e.password;
if (this.handleValidation()) {
fetch("Http://localhost:1234/api/CustomerManager/AuthenticateUser?UserName=" + userName +
"&Password=" + password, {
method: "GET",
headers: {
contentType: "application/json; charset=utf-8",
accept: 'application/json'
},
})
.then((response) => {
response.json();
})
.then((data) => {
this.state.fields = data;
console.log(data);
console.log("idAccount: {0}, UserFirstName: {1}, UserLastName: {2}",
this.state.fields["idAccount"], this.state.fields["UserFirstName"],
this.state.fields["UserLastName"]);
})
.catch((error) => {
console.log('error: ' + error);
});
}
else {
console.log('error: ' + this.state.errors);
}
};
handleValidation() {
let fields = this.state.inputFields;
let errors = {};
let formIsValid = true;
//UserName
if (!fields["userName"]) {
formIsValid = false;
errors["userName"] = "UserName Cannot be empty";
}
//Password
if (!fields["password"]) {
formIsValid = false;
errors["password"] = "Password Cannot be empty";
}
this.setState({ errors: errors });
return formIsValid;
};
handleChange(field, e) {
let fields = this.state.inputFields;
fields[field] = e.target.value;
this.setState({ fields });
};
render() {
return (
<>
<Header />
<div className="login center">
<form
onSubmit={this.authenticateUser.bind(this)}
>
<div>
<label htmlFor="userName">
Username:
</label>
</div>
<div>
<input
id="userName"
onChange={this.handleChange.bind(this, "userName")} value=
{this.state.inputFields["userName"]}
/>
</div>
<div>
<label htmlFor="password">
Password (case-sensitive):
</label>
</div>
<div>
<input
id="password"
type="password"
onChange={this.handleChange.bind(this, "password")} value=
{this.state.inputFields["password"]}
/>
</div>
<div>
<button className="Button ActionButton">Sign In</button>
</div>
</form>
</div>
</>
);
};
}
export default Login
Controller
[HttpGet]
public IHttpActionResult AuthenticateUser(string userName, string password)
{
try
{
Login.AuthenticateUser(userName, password);
SessionState sessionState = new SessionState
{
IdAccount = IdAccount,
IdAccountUser = IdAccountUser,
UserFirstName = UserFirstName,
UserLastName = UserLastName,
UserEmail = UserEmail,
UserCulture = UserCulture,
};
return Ok(sessionState);
}
catch (Exception ex)
{
// show the error
return InternalServerError(ex);
}
}
【问题讨论】:
-
您能否在浏览器中访问您的 API 端点,并检查响应是 JSON 而不是 XML。
-
当 API 端点与 React 应用程序在同一个项目中时,你如何做到这一点?我不知道该怎么做。我正在使用 VS 2019
-
在浏览器中访问您的
/api/CustomerManager/AuthenticateUser?UserName=xxx&Password=xxxenppint。在前面加上localhost:port -
我没有得到任何回报。我查看了开发工具以查看请求的请求标头,它没有 json 作为选项之一。这就是它所拥有的。 text/html,application/xhtml+xml,application/xml;q=0.9,image/avif,image/webp,image/apng,/;q=0.8,application/signed-exchange;v =b3;q=0.9
-
你的构造函数中有 2 个状态对象,第二个状态赋值覆盖第一个赋值
标签: reactjs asp.net-core asp.net-web-api