无法从localhost

时间:2018-03-13 03:56:53

标签: javascript reactjs jira-rest-api

我正在尝试使用JIRA在REACT中提供的REST API进行登录。 但我得到的错误就像

Response to preflight request doesn't pass access control check: No 'Access-Control-Allow-Origin' header is present on the requested resource. 
Origin 'http://localhost:8085' is therefore not allowed access. The response had HTTP status code 403. 
If an opaque response serves your needs, set the request's mode to 'no-cors' to fetch the resource with CORS disabled.

以下是我正在测试的代码。

fetch("https://jira.company.com/rest/auth/latest/session",{
  method:'POST',
  headers: {
    "Content-Type": "application/json",
    "Access-Control-Allow-Origin" : "*"
  },
  body: 
    JSON.stringify({
    username:"username",
    password : "password"})
  }).then(result => console.log(result));

我也尝试使用以下参数但结果却出现同样的错误。

'Access-Control-Allow-Origin' : '*',
'Access-Control-Allow-Methods': 'POST, GET, OPTIONS',

我还查看了JIRA API的文档,但找不到任何跨域请求访问权限。 寻求跨域执行API调用的帮助。

1 个答案:

答案 0 :(得分:1)

尝试设置mode: 'cors'

fetch("https://jira.company.com/rest/auth/latest/session", {
  method: 'POST',
  headers: {
    "Content-Type": "application/json",
    "Access-Control-Allow-Origin": "*"
  },
  mode: 'cors',
  body: JSON.stringify({
    username: "username",
    password: "password"
  })
}).then(function(resp) {
  return resp.json();

}).then(function(data) {
  console.log(data)
});

注意:在演示中,您可能会看到404表示未找到,因为它没有发送用户名&密码