简体   繁体   English

使用Bootstrap 3.x模式的Django用户登录的Ajax问题

[英]Ajax issue of Django user login using bootstrap 3.x modal

My purpose is once modal form submitted, ajax pass the username and password to a view and get back the json info to update the modal form, I trace the djanog code ,and 100% sure the JsonResponse(rsdic) has been executed in server side, as well as client fully receive the son info with ajax success function. 我的目的是提交模态表单后,ajax将用户名和密码传递给视图,并获取json信息以更新模态表单,我跟踪djanog代码,并100%确保JsonResponse(rsdic)已在服务器端执行,以及带有ajax成功功能的客户端完全接收儿子信息。

The problem is how to update the #error_message1 field which embeded in model. 问题是如何更新嵌入在模型中的#error_message1字段。 I tried .html() or .append(), all failed. 我尝试了.html()或.append(),但都失败了。

I leave the core coding for your reference, many thanks again for your help again. 我将核心代码留给您参考,再次感谢您的帮助。

ajax component: ajax组件:

 $.ajax( { type: "POST", url: url, data: { 'username': username, 'password': password }, dataType: 'json', success: function(data){ $('#login-form')[0].reset(); if (data.ret != '1107'){ var htmlcode ="<p> data.info </p>"; $('#modal').find('.error_message1').append(htmlcode); } }, error: function (data) { console.log('An error occurred.'); console.log(data); }, }); 

html component: html组件:

 <div class="modal fade" id="modal"> <div class="modal-dialog" role="document"> <div class="modal-content"> <div id="form-modal-body" class="modal-body"> <button type="button" class="close" data-dismiss="modal" aria-hidden="true">&times</button> <h4>EXP LOGIN</h4> <form role="form" action="{% url 'auth_login' %}" method="post" id="login-form"> {% csrf_token %} <ul> <li> <div class="g-icon"><i class="iconfont icon-yonghu"></i></div> <input type="text" name="username" id="username" value="" placeholder="User name" /> </li> <li> <div class="g-icon"><i class="iconfont icon-mima"></i></div> <input type="password" name="password" id="password" value="" placeholder="Password" onblur="check1()" /> <div class="g-cue" id="error_message1">Your email or password was entered incorrectly.</div> </li> </ul> <div class="g-btn"> <input class="g-submit" id='login-button' type="submit" value="{% trans 'Log in' %}" /> <input type="hidden" name="next" value="{{ next }}" /> </div> <p><span>{% trans "Not a member?" %} <a href="{% url 'registration_register' %}">Join now.</a></span></p> </form> </div> </div> </div> </div> 

你有error_message1作为id不是

$('#modal').find('#error_message1').html(htmlcode);

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

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