Java 角度/弹簧启动|错误:SyntaxError:“错误”;JSON.parse:JSON数据第1行第1列的意外字符;
我目前正在编写一个教程来了解SpringBoot,目前面临以下问题 在我的注册过程中(正确工作->用户在数据库中结束),我在浏览器控制台中获得状态代码200/OK,但也收到一条关于错误语法的错误消息: 我的后端代码如下所示: 授权控制器:Java 角度/弹簧启动|错误:SyntaxError:“错误”;JSON.parse:JSON数据第1行第1列的意外字符;,java,angular,spring,typescript,spring-boot,Java,Angular,Spring,Typescript,Spring Boot,我目前正在编写一个教程来了解SpringBoot,目前面临以下问题 在我的注册过程中(正确工作->用户在数据库中结束),我在浏览器控制台中获得状态代码200/OK,但也收到一条关于错误语法的错误消息: 我的后端代码如下所示: 授权控制器: @RestController @RequestMapping(value = "/api/auth") @AllArgsConstructor public class AuthController { private fi
@RestController
@RequestMapping(value = "/api/auth")
@AllArgsConstructor
public class AuthController {
private final AuthService authService;
private final RefreshTokenService refreshTokenService;
@PostMapping(value = "/signup")
public ResponseEntity<String> signup(@RequestBody RegisterRequest registerRequest) {
/*
* RegisterRequest: Through this class we are transferring the user details like username, password and email as part of the RequestBody (DTO)
* */
authService.signUp(registerRequest);
return new ResponseEntity<>("Registration Successful", null, OK);
}
....
使用的DTO:
public class RegisterRequest {
private String email;
private String username;
private String password;
}
我的前端代码如下所示:
注册组件:
signUp() {
this.signUpRequestPayload.email = this.signUpForm.get('email').value;
this.signUpRequestPayload.password = this.signUpForm.get('password').value;
this.signUpRequestPayload.username = this.signUpForm.get('username').value;
this.authService.signUp(this.signUpRequestPayload).subscribe((data) => {
console.log('Sign up successful', data);
});
}
授权服务:
@Transactional
public void signUp(RegisterRequest registerRequest) {
User user = new User();
user.setUsername(registerRequest.getUsername());
user.setEmail(registerRequest.getEmail());
user.setPassword(passwordEncoder.encode(registerRequest.getPassword()));
user.setCreated(now());
user.setEnabled(false);
userRepository.save(user);
String token = generateVerificationToken(user);
String message = mailContentBuilder.build("Thank you for signing up to Spring Reddit, please click on the below url to activate your account : "
+ ACTIVATION_EMAIL + "/" + token);
mailService.sendMail(new NotificationEmail("Please Activate your account", user.get
Email(), message));
}
@Injectable({
providedIn: 'root'
})
export class AuthService {
private headers = new HttpHeaders({ 'Content-Type': 'application/json' });
constructor(private http: HttpClient) { }
signUp(signUpRequestPayload: SignUpRequestPayload): Observable<SignUpRequestPayload> {
const body = JSON.stringify(signUpRequestPayload);
return this.http.post<SignUpRequestPayload>('http://localhost:8080/api/auth/signup', body, { headers: this.headers });
}
}
我做错了什么?因为您的响应(“注册成功”)无效
所以请从下面的行中删除
return this.http.post<SignUpRequestPayload>
返回this.http.post
我是这样解决的:
signUp(signUpRequestPayload: SignUpRequestPayload): Observable<string> {
const body = signUpRequestPayload;
return this.http.post('http://localhost:8080/api/auth/signup', body, { responseType: 'text', headers: this.headers });
}
signUp(signUpRequestPayload:signUpRequestPayload):可观察{
const body=signUpRequestPayload;
返回此.http.post('http://localhost:8080/api/auth/signup,正文,{responseType:'text',标题:this.headers});
}
我不得不从post方法中删除,并将responseType设置为“text”。我还必须删除JSON.stringify()方法,并将返回类型设置为Observable。这取决于您的响应类型,您可以在此时删除它以使用string。我尝试完全删除它,并用string-->相同错误替换该类型。。
signUp(signUpRequestPayload: SignUpRequestPayload): Observable<string> {
const body = signUpRequestPayload;
return this.http.post('http://localhost:8080/api/auth/signup', body, { responseType: 'text', headers: this.headers });
}