我正在使用 IBM Watson 工具中的音调分析器 api。当邮递员对其进行测试时,它工作正常,但在 Angular 中,它以 500 状态返回错误。作为音调分析器 api,它需要授权,我将凭据放入标头中。
这是我的代码示例。
服务.ts:
const TONE_API = "https://gateway.watsonplatform.net/tone-analyzer/api/v3/tone?version=2017-09-21&text=";
const httpOptions = {
headers: new Headers({
'Content-Type': 'application/json',
'Access-Control-Allow-Origin':'*',
'Access-Control-Allow-Headers': 'Origin, X-Requested-With, Content-Type, Accept, Authorization',
'Access-Control-Allow-Methods': 'GET, POST, PUT, DELETE, OPTIONS',
'Authorization': 'Basic ************************:************'
})
};
const options: RequestOptionsArgs = new RequestOptions();
options.headers = httpOptions.headers;
@Injectable()
export class ToneAnalyserService {
constructor(private http: Http) { }
// API: GET /message
getAnalyser(message:string) {
let url = TONE_API + message;
return this.http.get(url, options)
.map(res => res.json())
.catch(err=>{
throw(err);
});
}
组件.ts:
this.message = "I like this idea!";
this.toneAnalyser.getAnalyser(this.message).subscribe(res =>{
this.result = res;
console.log(res.json());
}, err => {
console.log(err);
});