我正在使用和Django Rest Framework + Django CORS Headers在AngularJS中开发一个1页的应用程序.
我的问题是,当我联系后端时,"csrftoken"cookie永远不会出现在我的浏览器中.
例如:我正在使用帖子进行登录.我正确地获得了"sessionid"cookie,但是"csrftoken"从未出现,因此我无法从我的客户端发出适当的帖子,因为我因为缺少csrf令牌而被拒绝.
前/后端的一些代码片段.这些是未完成的片段,所以不要挂在写得不好的代码上.
class LoginView(APIView):
renderer_classes = (JSONPRenderer, JSONRenderer)
def post(self, request, format=None):
serializer = LoginSerializer(data=request.DATA)
if serializer.is_valid():
userAuth = authenticate(username=serializer.data['username'], password=serializer.data['password'])
if userAuth:
if userAuth.is_active:
login(request, userAuth)
loggedInUser = AuthUserProfile.objects.get(pk=1)
serializer = UserProfileSerializer(loggedInUser)
user = [serializer.data, {'isLogged': True}]
else:
user = {'isLogged': False}
return Response(user, status=status.HTTP_200_OK)
return Response(serializer.errors, status=status.HTTP_400_BAD_REQUEST)
Run Code Online (Sandbox Code Playgroud)
.controller('LoginCtrl', ['$scope', '$http', 'uService', '$rootScope', function(scope, $http, User, rootScope) {
scope.login = function() { …Run Code Online (Sandbox Code Playgroud) django cors angularjs django-rest-framework django-cors-headers
我正在编写一个AngularJS应用程序,在这个应用程序中有一个域管理工具.选择域后,我会在表中生成域记录,让用户编辑每一行.由于这些字段是动态创建的,因此我使用ng-form来单独验证每一行,因为它们共享相同的名称.
每个域记录都有一个内容字段,其中包含IP,CNAME等.我使用根据选择的记录类型(A,CNAME,TXT等)的函数生成的正则表达式模式验证此字段.
问题是,当我编辑一个A记录,然后将记录类型更改为CNAME时,表单仍然显示有效,因为没有执行内容字段的新验证.我重新验证它的唯一方法是开始输入内容字段,然后工作正常.
查看下面的图片:
我在A记录上按编辑,一切看起来都很好:

我将记录类型更改为CNAME,即使正则表达式发生更改,表单仍然显示有效.当我更改类型时,我希望重新验证内容(1.2.3.4),因为它是一个新的正则表达式:

我开始在内容字段中输入,现在表单正确无效.我希望在我更改记录类型时发生这种情况,而不仅仅是当我再次开始输入时:

#Slim version of the template to give you an idea on whats going on
<form novalidate name="recordForm" class="css-form" ng-controller="EditRecordCtrl">
<table>
<tr ng-repeat="record in domain.data.0" class="gradeA">
<td ng-init="startingTypeData = record.type" class="domains-td" ng-switch on="record.edit">
<span ng-switch-default>{{record.type}}</span>
<span ng-switch-when="true">
<select ng-change="domainRecordContentType(record.type)" ng-init="domainRecordContentType(record.type)" ng-model="record.type" ng-options="c for c in domainRecordTypes" class="span12">
</select>
</span>
</td>
<td ng-init="startingContentData = record.content" class="domains-td validation-dropdown-error-parent" ng-switch on="record.edit">
<span ng-switch-default>{{record.content}}</span>
<span ng-switch-when="true">
<ng-form name="innercContentForm">
<span class="validation-dropdown-error" ng-show="innercContentForm.content.$error.pattern">
{{ 'RECORD_EDIT_FORM_RECORD_CONTENT_PATTERN_MSG' | translate }} ( {{ …Run Code Online (Sandbox Code Playgroud) 我使用ng-pattern来验证我的客户端是否有一些情况,比如查看密码复杂性.
现在,我使用ng-pattern在我的正则表达式中得到了非常奇怪的匹配.请看下面的例子
<span class="registration-error" ng-show="regForm.password.$error.pattern">- Fail to match..</span>
<input type="password" ng-model="registerForm.password" name="password" ng-pattern="/^.*(?=.{8,})(?=.*[a-z])(?=.*[A-Z])(?=.*[\d\W]).*$/" required/>
Run Code Online (Sandbox Code Playgroud)
以上应该需要8个字符,至少1个大写字符和1个数字或特殊字符.出于某种原因,我得到像FaiLoudD这样的单词的匹配,这是没有意义的.
我在我的python后端运行相同的正则表达式,它就像一个魅力.我还使用http://regexpal.com/验证了正则表达式 ,它也很完美.
为什么ng-pattern匹配如此奇怪?
我挖掘了我的应用程序的所有模块和整个构建过程.当我在构建过程中禁用grunt-html2js时,ng-pattern指令再次开始工作.它似乎导致ng-pattern错误地解释了正则表达式.问题是在html2js项目中创建的.有了这个,我将结束这个问题.