我的 Angular 应用程序在http://localhost:4200端口上运行,Spring Boot 在http://localhost:8080端口上运行。当我从客户端 PUT 请求时,我收到此错误。
我尝试通过执行以下操作来创建标题:
headers = new HttpHeaders({'Access-Control-Allow-Origin' : '*'})
Run Code Online (Sandbox Code Playgroud)
然后将 headers 变量放入 PUT 请求中,如下所示:
this.http.put("//localhost:8080/save-account", accountForm,
{headers: this.headers}).subscribe(
res => console.log(res),
err => console.log(err)
)
Run Code Online (Sandbox Code Playgroud)
但没有任何结果。
我的代码:
服务器端控制器:
package com.tigran.chagmo.controllers;
import com.tigran.chagmo.models.AccountRepository;
import java.util.Collection;
import com.tigran.chagmo.entities.Account;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
@RestController
@CrossOrigin(origins="http://localhost:4200")
public class HomeController{
@Autowired
AccountRepository accRepo;
@PutMapping("/update-account")
public Account putAccount(@RequestBody Account account){
accRepo.save(account); …Run Code Online (Sandbox Code Playgroud)