我在 ember js 中有设计适配器,它向后端发送请求以获取数据,但出现错误
“XMLHttpRequest 无法加载 https://example.com/api。请求的资源上不存在“Access-Control-Allow-Origin” header 。因此不允许访问来源“http://localhost:4200” ."
在控制台中。
如果我在我的服务器上的 nginx 配置文件中使用以下代码,我可以解决这个问题。后端是使用 django 设计的。
if ($request_method = 'GET') {
add_header 'Access-Control-Allow-Origin' '*';
add_header 'Access-Control-Allow-Credentials' 'true';
add_header 'Access-Control-Allow-Methods' 'GET, POST, OPTIONS';
add_header 'Access-Control-Allow-Headers' 'DNT,X-CustomHeader,Keep-Alive,User-Agent,X-Requested-With,If-Modified-Since,Cache-Control,Content-Type';
}
但我不想打扰 tha nginx 配置。
适配器
import DS from 'ember-data';
export default DS.RESTAdapter.extend({
host:"https://example.com/api/",
});
如何发送请求以免显示任何错误
最佳答案
不幸的是,您将必须编辑服务器的配置文件。否则,您必须从与后端相同的域和端口来提供应用程序。
关于javascript - 无法在 ember 中向后端服务器发出请求(错误 :'No Access-Control-Allow-Origin' ),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33587349/