Django 爱奥尼亚和德扬戈:否';访问控制允许原点';标题已存在
我正在用Ionic构建我的第一个应用程序,并使用Django Rest框架作为API 我只想在Ionic中创建一个简单的页面,显示类别列表 我已经为API创建了模型类别和视图集。当我转到Django rest framwork viewer()时,一切都正常 但当我尝试(使用Ionic)获得结果时,我得到了以下错误:Django 爱奥尼亚和德扬戈:否';访问控制允许原点';标题已存在,django,api,ionic-framework,django-rest-framework,ionic2,Django,Api,Ionic Framework,Django Rest Framework,Ionic2,我正在用Ionic构建我的第一个应用程序,并使用Django Rest框架作为API 我只想在Ionic中创建一个简单的页面,显示类别列表 我已经为API创建了模型类别和视图集。当我转到Django rest framwork viewer()时,一切都正常 但当我尝试(使用Ionic)获得结果时,我得到了以下错误: XMLHttpRequest cannot load http://localhost:3010/category/. No 'Access-Control-Allow-Origi
XMLHttpRequest cannot load http://localhost:3010/category/. No 'Access-Control-Allow-Origin' header is present on the requested resource. Origin 'http://localhost:8100' is therefore not allowed access.
我的代码:
import {Page, Platform, NavController} from 'ionic/ionic';
import {EmployeeDetailsPage} from '../employee-details/employee-details';
import {Http} from 'angular2/http';
@Page({
templateUrl: 'build/pages/getting-started/getting-started.html'
})
export class GettingStartedPage {
constructor(platform: Platform, nav: NavController, http: Http) {
this.platform = platform;
this.nav = nav;
this.http = http;
this.http.get("http://localhost:3010/category/")
.subscribe(data =>{
this.items=JSON.parse(data._body).results;//Bind data to items object
},error=>{
console.log(error);// Error getting the data
} );
}
}
您可能需要安装
如果您在chrome上进行测试,您可能还需要允许跨源(有一个扩展),这将帮助您解决CORS问题。您可能需要安装
如果您在chrome上进行测试,您可能还需要允许跨源(它有一个扩展),这将帮助您解决CORS问题。您需要将CORS头发送到Ionic Framework,以允许跨域ajax工作,因为您的Ionic应用程序托管在端口8100上,django服务器运行在端口3010上,这被视为跨域请求 对于django,通过pip安装应用程序 在您的设置中,启用“corsheaders”应用程序,并添加允许的URL
# this disables Cross domain requests
CORS_ORIGIN_ALLOW_ALL = False
# this allows cookie being passed cross domain
CORS_ALLOW_CREDENTIALS = True
# this is the list of allowed origins for cross domain ajax
CORS_ORIGIN_WHITELIST = (
'localhost:8100',
)
您需要将CORS头发送到Ionic Framework以让跨域ajax工作,因为您的Ionic应用程序托管在端口8100上,而django服务器运行在端口3010上,这被视为跨域请求 对于django,通过pip安装应用程序 在您的设置中,启用“corsheaders”应用程序,并添加允许的URL
# this disables Cross domain requests
CORS_ORIGIN_ALLOW_ALL = False
# this allows cookie being passed cross domain
CORS_ALLOW_CREDENTIALS = True
# this is the list of allowed origins for cross domain ajax
CORS_ORIGIN_WHITELIST = (
'localhost:8100',
)
谢谢关于扩展的提示。谢谢关于扩展的提示。可能的重复可能的重复