使用Angular服务的飞行前Api错误?

时间:2018-01-16 14:54:30

标签: angular http-post angular2-services

我必须在标题中发送授权令牌并通过post调用从服务器获取数据,它会抛出错误。在标头中发送属性以呈现响应的正确方法是什么?

search.service.ts

import {Injectable} from '@angular/core';
import {HttpClient} from '@angular/common/http';
import 'rxjs/add/operator/map';

@Injectable()
export class SearchService {

    constructor(private http: HttpClient) {}
    getSearch(searchObj: {}) {
        const config = {
            headers: {
                'Authorization': 'Basic bTA5NDRpb25idXMuYXTVNAMjAxNw==',
                'Content-Type': 'text/plain'
            },
            data: searchObj
        };
        console.log("searchObj", searchObj)
        return this.http.post('http://service/getHistoricEvents', JSON.stringify(config))
            //.map(res => res.json());
            .map(res => console.log(res));
    }

}

ERROR

Failed to load http://service/getHistoricEvents: Response for preflight is invalid (redirect)
core.js:1350 ERROR HttpErrorResponse {headers: HttpHeaders, status: 0, statusText: "Unknown Error", url: null, ok: false, …}

0 个答案:

没有答案