无法为Angular 4中的Http设置标头

无法为Angular 4中的Http设置标头,angular,http,header,Angular,Http,Header,在login.service.ts中,我有一个名为getStudent()的方法,在该方法中,我将JWT令牌设置为头并访问后端(Spring boot)。但是,我总是收到缺少标题的消息。这是我的代码: login.service.ts import { Injectable } from '@angular/core'; import { Http, HttpModule, Response } from '@angular/http'; import 'rxjs/add/operator/ma

在login.service.ts中,我有一个名为getStudent()的方法,在该方法中,我将JWT令牌设置为头并访问后端(Spring boot)。但是,我总是收到缺少标题的消息。这是我的代码: login.service.ts

import { Injectable } from '@angular/core';
import { Http, HttpModule, Response } from '@angular/http';
import 'rxjs/add/operator/map';
import { Observable } from 'rxjs/Observable';
import { Subject } from 'rxjs/Subject';
import { ConnectionService } from './connection.service';
import { RequestOptions } from '@angular/http';
import { CookieService } from 'ngx-cookie-service';

declare var $: any;
@Injectable()
export class LoginService {
  private loginState = new Subject<any>();
  public headers: any;


  constructor(private http: Http, private connectionLink: ConnectionService, private cookie:CookieService) {
   }

  getStudent(id: string) {
    let options:any = new RequestOptions();
    let headers: any = new Headers();
    headers.append('Content-Type', 'application/json');
    headers.append('Authorization', 'Bearer ' + this.cookie.get('token'));
    options = new RequestOptions({ headers: headers });

    return this.http.get('http://localhost:8080/getStudentinfoByIDREST/'+id,options)
    .toPromise()
    .then(res =>res.json())
  }
}

我确信我的后端工作正常,因为我与邮递员进行了多次测试。因此,我假设我的代码有问题。请帮帮我,谢谢

将此附加到标题中
headers.append('Access-Control-Allow-Origin','*')

因为cors的错误在我看来是服务器配置的问题

“请求的资源上不存在“Access Control Allow Origin”标头”表示不允许您发出请求的源(此处localhost:4200)访问您的api

也许您可以尝试在请求的api端点中使用@CrossOrigin注释。此注释允许注释端点上的所有原点。通过这种方式,您将看到这是否真的是配置的问题


希望有帮助

这没有影响,
访问控制允许源代码
需要在后端设置。您遇到了什么错误?你能给我发一个屏幕短的错误吗我不是OP,我只是说:)是的,它实际上不起作用。我得到的错误(当我使用chrome控制台进行检查时)是请求的资源上不存在“访问控制允许来源”标题。因此,不允许访问源“”。响应的HTTP状态代码为500。我在后端更新了我的CorsConfig.java,你们可以看一看。我可以做些什么来修复它?你们应该更改服务器配置,以允许从源站请求api。你确定你的CorsConfig真的被使用了吗?我不确定,但我不知道怎么检查。有什么方法可以检查吗?我用一种快速检查的方法编辑了我的响应。问题来自服务器配置:)我已经使用了@CrossOrigin注释,但结果仍然是一样的。
import org.springframework.boot.context.embedded.FilterRegistrationBean;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.cors.CorsConfiguration;
import org.springframework.web.cors.UrlBasedCorsConfigurationSource;
import org.springframework.web.filter.CorsFilter;
import org.springframework.web.servlet.config.annotation.CorsRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurerAdapter;

@Configuration
public class CorsConfig {

    @Bean
    public FilterRegistrationBean corsFilter() {
        UrlBasedCorsConfigurationSource source = new UrlBasedCorsConfigurationSource();
        CorsConfiguration config = new CorsConfiguration();
        config.setAllowCredentials(true);
        config.addAllowedOrigin("*");
        config.addAllowedHeader("*");
        config.addAllowedMethod("OPTIONS");
        config.addAllowedMethod("HEAD");
        config.addAllowedMethod("GET");
        config.addAllowedMethod("PUT");
        config.addAllowedMethod("POST");
        config.addAllowedMethod("DELETE");
        config.addAllowedMethod("PATCH");
        source.registerCorsConfiguration("/**", config);
        // return new CorsFilter(source);
        final FilterRegistrationBean bean = new FilterRegistrationBean(new CorsFilter(source));
        bean.setOrder(0);
        return bean;
    }

    @Bean
    public WebMvcConfigurer mvcConfigurer() {
        return new WebMvcConfigurerAdapter() {
            public void addCorsMappings(CorsRegistry registry) {
                registry.addMapping("/**").allowedMethods("GET", "PUT", "POST", "GET", "OPTIONS");
            }
        };
    }
}