简体   繁体   English

离开/ angularjs与django csrf在本地服务器上工作但不在设备上工作

[英]ionic/angularjs with django csrf works on local server but not working on device

I am working on ionic and use Django with django-rest-framework as backend. 我正在研究离子并使用Djangodjango-rest-framework作为后端。 I tried to post to my server with $http as following: 我尝试使用$http发布到我的服务器,如下所示:

$http({
  url: url,
  method: method,
  data: {
    // some data
  }
}).success...

Also I set the following in my config: 我还在配置中设置了以下内容:

$httpProvider.defaults.xsrfCookieName = 'csrftoken';
$httpProvider.defaults.xsrfHeaderName = 'X-CSRFToken';

I also tried ngCookies but $cookies.csrftoken does not return anything. 我也试过ngCookies但是$cookies.csrftoken没有返回任何内容。

Here is my backend code: 这是我的后端代码:

class Mark(APIView):
    def post(self, request):
        # do something
        return Response({
            # something
        })

This works perfect when I used ionic local server ionic serve , but when I build it on iOS emulator or on my iPhone, csrf 403 appears: 当我使用离子本地服务器ionic serve ,这是完美的,但是当我在iOS模拟器或我的iPhone上构建它时,csrf 403出现:

{"detail":"CSRF Failed: CSRF token missing or incorrect."}

returned from the server. 从服务器返回。

Any idea on this? 有什么想法吗? Thank you so much! 非常感谢!

I do not think this information will fix your problem you will probably face it later. 我不认为这些信息可以解决你的问题,你可能会在以后遇到这个问题。 On iOS cookies are lost when rebooting the application, use window.localStorage to store them. 在重新启动应用程序时,iOS会丢失,请使用window.localStorage来存储它们。

See answer: Can you use cookies in a Cordova application? 查看答案: 您可以在Cordova应用程序中使用cookie吗?

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM