Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/413.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/jquery/89.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript Ajax、jQuery、Django。这是如何设置的?_Javascript_Jquery_Ajax_Django - Fatal编程技术网

Javascript Ajax、jQuery、Django。这是如何设置的?

Javascript Ajax、jQuery、Django。这是如何设置的?,javascript,jquery,ajax,django,Javascript,Jquery,Ajax,Django,以下是我的看法: def planning(request): if not request.user.is_authenticated(): return HttpResponseRedirect(reverse('loginregistration.views.login')) if request.is_ajax: POST = request.POST msg = "Success" print reque

以下是我的看法:

def planning(request):
    if not request.user.is_authenticated():
        return HttpResponseRedirect(reverse('loginregistration.views.login'))

    if request.is_ajax:
        POST = request.POST
        msg = "Success"
        print request.POST
        return HttpResponse(msg)
    else:
        form = planForm()
        return render(request, 'plan.html', {'form':form})
这是我的html:

{% extends "base.html" %}
{% block title %}Plan{% endblock %}

{% block content %}
    <a>Welcome to planning</a>
    {{form.as_p}}
    <script>
     <----- I need to know how to set this up ---->
  </script>
{% endblock %}
{%extends“base.html”%}
{%block title%}计划{%endblock%}
{%block content%}
欢迎来到规划
{{form.as_p}}
{%endblock%}
我知道我需要做一个按钮,当点击时提交我的表单,但从我在互联网上看到的例子来看,这不是很直观。有人能给我举个例子,告诉我如何在这个页面上找到一个按钮,将ajax调用发送到服务器

编辑: 我怎么把它挂在按钮上

{% extends "base.html" %}
{% block title %}Plan{% endblock %}

{% block content %}
    <a>Welcome to planning</a>
    {{form.as_p}}
    <button id="form_submit">Submit</button>
    <script>
        $('button#form_submit').click(function() {
        $.ajax({
            type: "POST",
            url:"/planning/submit/",
            data: {
                'test': 'success',
            },
            success: function(){
                alert('test')
            },
            error: function(){
                alert("Error");
        });
    </script>
{% endblock %}
{%extends“base.html”%}
{%block title%}计划{%endblock%}
{%block content%}
欢迎来到规划
{{form.as_p}}
提交
$('button#form_submit')。单击(函数(){
$.ajax({
类型:“POST”,
url:“/planning/submit/”,
数据:{
“测试”:“成功”,
},
成功:函数(){
警报(“测试”)
},
错误:函数(){
警报(“错误”);
});
{%endblock%}

实现这一点的方法有很多,但这里有一种,请记住您对
属性没有太多控制权:

您可以制作如下按钮:

<button id="form_submit">Submit</button>

看起来您只是缺少了上一个
};
,您应该序列化表单数据以发送它(将
表单id
替换为表单id)。

request.is_ajax!=request.method==“POST”:也就是说,它不能使用$。POST请参阅更新的答案。看起来您只是缺少了代码块的结尾。
$('button#form_submit').click(function() {
    $.ajax({
        type: "POST",
        url:"/planning/submit/",
        data: $("#form_id").serialize(),
        success: function(){
            alert('test')
        },
        error: function(){
            alert("Error");
    });
});