【发布时间】:2018-06-14 03:17:15
【问题描述】:
我有用 AngularJS 编写的应用程序。我想访问 FlightAware API。我使用以下代码:
let Client = require('node-rest-client').Client;
let username = 'x';
let apiKey = 'y';
let fxmlUrl = 'https://flightxml.flightaware.com/json/FlightXML3/'
let client_options = {
user: username,
password: apiKey
}
let client = new Client(client_options);
client.registerMethod('findFlights', fxmlUrl + 'FindFlight', 'GET');
client.registerMethod('weatherConditions', fxmlUrl + 'WeatherConditions', 'GET');
let findFlightArgs = {
parameters: {
origin: 'KIAH',
destination: 'KJFK',
type: 'nonstop'
}
}
let weatherConditionsArgs = {
parameters: {
airport_code: 'KHOU'
}
}
client.methods.weatherConditions(weatherConditionsArgs, function (data, response) {
console.log('Current conditions at Hobby Airport: %j', data.WeatherConditionsResult.conditions[0]);
})
当我在 nodeJS 中执行它时,它可以工作,但在我的 AngularJS 应用程序中我得到一个错误:
Request header field Authorization is not allowed by Access-Control-Allow-Headers in preflight response.
我发现一些信息表明此错误与 CORS 相关,但无论我使用 AngularJS 还是 NodeJS 来运行代码,服务器端都是相同的。如何在 AngularJS 中执行而不出错?
【问题讨论】:
-
检查请求头是否包含以下内容:
Accept: application/xml? -
我试过了,但没有帮助。顺便说一下,响应是json格式的。
标签: javascript angularjs node.js rest