Javascript 无法从本地主机执行跨域REST API调用
我正在尝试使用JIRA在REACT中提供的RESTAPI进行登录。 但我得到的错误就像Javascript 无法从本地主机执行跨域REST API调用,javascript,reactjs,jira-rest-api,Javascript,Reactjs,Jira Rest Api,我正在尝试使用JIRA在REACT中提供的RESTAPI进行登录。 但我得到的错误就像 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
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',
我还查阅了的文档,但找不到任何跨域请求访问的文档。
正在寻找跨域执行API调用的帮助 尝试设置
模式:“cors”
fetch(“https://jira.company.com/rest/auth/latest/session", {
方法:“POST”,
标题:{
“内容类型”:“应用程序/json”,
“访问控制允许来源”:“*”
},
模式:“cors”,
正文:JSON.stringify({
用户名:“用户名”,
密码:“密码”
})
}).然后(功能(resp){
返回resp.json();
}).then(功能(数据){
console.log(数据)
});代码>RESTAPI不打算从浏览器中调用(也称为“无访问控制允许源代码”标题)。不过,您可以从服务器上获取它,例如cURL。顺便说一句,用户名/pwd通常以btoa
的标题发送。谢谢Ron,因为这些API受到保护,无法通过浏览器访问。有没有办法从浏览器中调用受保护的API?我用mode:'cors'尝试过,但没有解决我的问题。我曾尝试使用POSTMAN执行相同的API调用,但在请求中未提供模式的情况下,该调用运行良好。您是否仍看到403?是的,仍然看到403。在Chrometh中检查请求时,获取“XSRF检查失败”。这可能与cors不同