简体   繁体   English

Ajax反馈提交,无需重新加载页面

[英]ajax feedback submit without page reload

I am new to ajax and i am trying to submit a form and the page should not reload. 我是ajax的新手,我正在尝试提交表单,因此该页面不应重新加载。 I am doing it in django. 我在django上做。

html html

#...
<form id="feedback_submit_form" action="" method="post">
{%csrf_token%}
<textarea id="feedback_text" name="feedback_text"></textarea>
<input type="submit" id="feedback_submit" value="Send feedback"/>
</form>
#...

javascript javascript

$('#feedback_submit_form').submit(function(e){
$.post('**WHAT URL SHOULD BE HERE**', $(this).serialize(), function(data){
 ***WHAT SHOULD I DO HERE***
   });
e.preventDefault();
});

views.py views.py

def ajax_view(request):
    if request.method=='POST':
        feedback=request.POST.get('feedback_text')
        #save feedback to database
        **DO I NEED A RETURN HERE**

I want to do that the form for the feedback is submitted and the current page should not refresh and the data in the feedback form should be saved in to the database. 我要提交反馈表单,并且不应刷新当前页面,并且将反馈表单中的数据保存到数据库中。

How can i do it. 我该怎么做。 Hope u understood it. 希望你能理解。

Thanks in advance... 提前致谢...

The URL can be whatever you want: one solution is to use the form's action attribute with $(this).action . 该URL可以是您想要的任何一种:一种解决方案是将表单的action属性与$(this).action

You don't really specify what you want to happen, but one good pattern is for your view to render a partial template that formats the result, and the Ajax to simply insert that into the page with $(surrounding_div).html(result) . 您并没有真正指定要发生的事情,但是一个不错的模式是让视图呈现格式化结果的部分模板,而Ajax只需使用$(surrounding_div).html(result)将其插入页面即可。

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

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