![](/img/trans.png)
[英]' from origin 'http://localhost:3000' has been blocked by CORS policy: No 'Access-Control-Allow-Origin' header is present on the requested resource
[英]'http://localhost:3000' has been blocked by CORS policy: No 'Access-Control-Allow-Origin'
Can you help me, i have a function problematic: http://localhost:3000
has been blocked by CORS policy: No Access-Control-Allow-Origin
header is present on the requested resource. 如果不透明的响应满足您的需求,请将请求的模式设置为no-cors
以获取资源:
handleSubmit = e => {
e.preventDefault();
const data={
'username' : this.username,
'password' : this.password
}
fetch(process.env.REACT_APP_API_URL+'/api/login', data,{
method:"POST",
headers:{
'accept': 'application/json',
'Access-Control-Allow-Origin': "*",
'content-type': 'application/x-www-form-urlencoded',
'Access-Control-Allow-Credentials': 'true',
}
})
.then(r => r.json());
}
但是url有问题,怎么解决? 在此处输入图像描述
当您访问的port
与您正在使用的port
不同时,您将无法访问资源。
正如@Freestyle09 所评论的,您需要在后端启用 CORS 策略:
在 PHP 中:
header('Access-Control-Allow-Origin: *');
在 node.js(快递)中:
cors
package。var express = require('express');
var cors = require('cors');
var app = express();
app.use(cors());
这应该将标头设置为Access-Control-Allow-Origin: *
,如果要指定主机:
app.use(cors({
origin: 'http://yourapp.com'
}))
从这个答案中阅读更多信息:
谢谢大家的输入和回答,这个问题已经解决,并且正在运行。
这个问题很简单,我只是在pckage.json中添加
"proxy":"http://127.0.0.1:8000",
我在 axios fatch 中使用
axios({
url:'/api/login',
data:data,
method:"POST",
mode: 'no-cors',
headers:{
"Content-Type": "application/json",
"Access-Control-Allow-Origin": process.env.REACT_APP_API_URL,
"Access-Control-Request-Headers": 'Content-Type, Authorization'
}
})
.then(res => {
console.log(res);
})
.catch(err =>{
console.log(err);
})
这对我有用 谢谢大家 (n_n)
在 spring 引导中,您可以使用注释“@CrossOrigin”。 您将通过您的反应应用程序的 url 获取参数来源:
@CrossOrigin(origins = "http://localhost:3000",methods = RequestMethod.GET)
@GetMapping("/courses")
public Iterable<Course> getCourses() {
CourseService courseService=new CourseService();
return courseService.getAllCourses();
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.