我想将我的应用程序与JIRA集成,所以当我向JIRA服务器发送请求时,它返回错误调用' XMLHttpRequest无法加载https:// ********* / rest / API / 2 /项目。对预检请求的响应没有通过访问控制检查:否'访问控制 - 允许 - 来源'标头出现在请求的资源上。'
这是我的代码
$http({
method: "GET",
url: "https://***********/rest/api/2/project",
headers: {
'Content-Type': 'application/json',
'Access-Control-Allow-Origin': '*******',
'Access-Control-Allow-Methods': 'GET, POST, DELETE, PUT, OPTIONS',
'Access-Control-Allow-Headers':'X-Requested-With, Content-Type, X-Codingpedia,Authorization'
},
dataType: "jsonp",
}).then(function mySuccess(response) {
$scope.myWelcome = response.data;
console.log(response.data);
}, function myError(response) {
$scope.myWelcome = response.statusText;
console.log(response.statusText);
});
};
答案 0 :(得分:-1)
我不知道你是否有答案,但仍然.. 只需将其添加到您的api服务器中
app.use(function (req, res, next) {
if (req.method === "OPTIONS") {
// End CORS preflight request.
res.writeHead(204);
res.end();
}else{
req.fullurl = req.protocol + '://' + req.get('host') + req.originalUrl;
res.setHeader("Access-Control-Allow-Origin", "*");
res.setHeader("Access-Control-Allow-Headers", "Origin, X-Requested-With, Content-Type, Accept");
res.setHeader('Access-Control-Allow-Methods', 'GET, POST, OPTIONS, PUT, PATCH, DELETE');
res.setHeader('Access-Control-Allow-Credentials', true);
}
next()
});