spring - 基本 GET 请求失败 Angular 和 Spring
问题描述
我正在尝试从角度到弹簧提出一个简单的请求。我使用了裸骨弹簧网和弹簧安全性以及简单的 angular(9) 请求。
让我分享一下我的 Java 和 Angular 代码。
Java 代码
@RestController @CrossOrigin(origins = "http://localhost:4200")
public class Main {
@RequestMapping("/hello")
public Map<String,Object> home(){
Map<String, Object> model = new HashMap<String, Object>();
model.put("id", UUID.randomUUID().toString());
model.put("content", "Hello from Backend");
return model;
}
}
角端 auth.componen.ts 文件
import { HttpClient } from '@angular/common/http';
import { NgForm } from '@angular/forms';
import { Component } from '@angular/core';
@Component({
selector: 'app-auth',
templateUrl: './auth.component.html'
})
export class AuthComponent{
isLoginMode = true;
title = 'Demo';
greeting = {};
onSwitchMode(){
this.isLoginMode = !this.isLoginMode;
}
onSubmit(form: NgForm){
console.log(form.value);
form.reset();
}
constructor(private http: HttpClient){
http.get('http://localhost:8080/hello').subscribe(data => this.greeting = data);
}
}
HTML 端
<div style="text-align:center"class="container">
<h1>
Welcome {{title}}!
</h1>
<div class="container">
<p>Id: <span>{{greeting.id}}</span></p>
<p>Message: <span>{{greeting.content}}!</span></p>
</div>
</div>
我正在关注此链接,但每次收到此错误时:
我试图在网上搜索,但找不到答案。你能帮我继续吗?
解决方案
推荐阅读
- android - 添加 cordova.js 可防止地理位置(或任何其他)回调
- asp.net-mvc - 用于定义加载了错误路线的动作的 Stimulsoft 选项
- matlab - 将文本文件中的值列表读入 MATLAB
- python - 如何使用 Python 2 和 3 处理类似字节的值
- arrays - 来自 Instagram 的 Google 表格正则表达式
- c++ - 运行在主线程旁边侦听 tcp 连接的线程时出错
- laravel - Axios 没有发送 cookie
- python - Django:如何区分来自同一页面的不同页面的发布请求?
- react-native - 在 React Native 中显示和存储大量文本的建议
- lamar - 使用 Lamar 引导 C# 服务时如何调试和查找错误“NullReferenceException”的来源?