源“ http:// localhost:4200”的源已被CORS策略阻止:请求的资源上不存在“ Access-Control-Allow-Origin”标头

时间:2020-04-16 10:24:00

标签: java node.js angular jax-rs

我的客户端应用程序(前端)-Angular 我的服务器端应用程序(后端)-Java JAX-RS(jersy)

从前端到后端调用函数时出现此错误

我的角度服务代码

import { User } from './../model/user';
import { Injectable } from '@angular/core';
import { HttpClient, HttpHeaders } from '@angular/common/http';
import { Observable } from 'rxjs';

const headeroption = {
  headers: new HttpHeaders({ 'Content-Type': 'application/json' })
};

@Injectable({
  providedIn: 'root'
})
export class UserService {

  constructor(private http: HttpClient) { }

  url = 'http://localhost:9090/HealthCare2/services/';

  user: Observable<User[]>;

  //Testing
  test() {
    return this.http.get(this.url + 'user/test');
  }

我的Java JAX-RS控制器

@Path("/user")
public class UserController {

    UserService userService = new UserService();

    // Test function
    @GET
    @Path("/test")
    @Produces(MediaType.TEXT_HTML)
    public String testing() {

        return "Test Function is working";

    }
}

0 个答案:

没有答案