使用Django api连接angular2

2024-09-28 05:16:30 发布

您现在位置:Python中文网/ 问答频道 /正文

因此,我试图连接我的angular2 app托管在localhost:4200上的django api托管在localhost:8000,我已经有一个angular 1,6应用托管在本地主机:800管理登录和所有其他东西, 因此,在我的angular2应用程序中,我收到了存储在cookies中的令牌,我正试图使用它在头文件中将get请求发送到django api。你知道吗

ps:我已经检查了我的DjangoAPI,它目前允许访问所有服务器,没有例外。你知道吗

fetchUser(){
    console.log("Here");

let headers = new Headers({'Accept': 'application/json','X-CSRFToken': this.token});

this.http.get( 'http://localhost:8000/api/orgs', {headers: headers}).subscribe(
  (response: Response) => {
    const testResponse = response.json();
    this.outerLinks.data = testResponse;
    this.data =testResponse;
    this.dataChange.next(this.data);
  }
);

} 

所以我收到这个error

XMLHttpRequest cannot load localhost:8000/api/orgs. Response for preflight is invalid (redirect)

Tags: djangoapijsonapplocalhosthttpdataget
2条回答

您可以尝试允许控制允许原点:* 如果你用Chrome试试这个 https://chrome.google.com/webstore/detail/allow-control-allow-origi/nlfbmbojpeacfghkpbjhddihlkkiljbi?hl=en

修改您的设置.py通过添加以下代码:

CORS_ORIGIN_ALLOW_ALL = False

CORS_ORIGIN_WHITELIST = (
    'localhost:4200'
    '<YOUR_DOMAIN>[:PORT]',
)

相关问题 更多 >

    热门问题