Django的阿贾克斯,jQuery的不获取数据 [英] Django Ajax-Jquery does not fetch the data

查看:105
本文介绍了Django的阿贾克斯,jQuery的不获取数据的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我不能够得到id为输入文本字段的数据 CITY_NAME 从通过jQuery Ajax的方法的形式。

这是我弟妹的错误是NetworkError:403 FORBIDDEN - http://127.0.0.1:8000/dashboard。

我知道如何使用隐藏字段类型来获取数据,但不能在这里而且使用的选项,获取数据从隐藏字段现在是一个过时的方法。那么,怎样才能利用Django的AjaxJquery方法,我得到的数据。

HTML

 <表单类型=POST>
            {%csrf_token%}
            <输入类型=文本占位符=输入城市名称ID =CITY_NAME>
            <输入类型=按钮值=开ID =上>
            <输入类型=按钮值=关ID =关闭>
< /形式GT;
 

JS文件

  $(函数(){

    $(#上)。点击(函数(事件){
        。事件preventDefault();

        $阿贾克斯({
            键入:POST,
            网址:仪表板,
            数据 : {
                CITY_NAME:$(#CITY_NAME)VAL()。
                   },

               });
    });
});
 

View.py

  @login_required
高清仪表板(要求):
    CTX = {}

    如果request.is_ajax():
        CITY_NAME = request.POST ['CITY_NAME']
        打印CITY_NAME

    返回render_to_response(仪表盘/ dashboard.html,CTX,context_instance = RequestContext的(要求))
 

urls.py

  URL模式=模式('',
    URL(R'^仪表盘$,apps.dashboard.views.dashboard,名字='llumpps_dashboard'),

    )
 

解决方案

它缺少请求中的CSRF令牌。

您可以使用 @csrf_exempt 装饰您的观点,如:

  @login_required
@csrf_exempt
高清仪表板(要求):
    ...
 

或者随请求发送令牌:

  $。阿贾克斯({
    键入:POST,
    网址:仪表板,
    数据 : {
        csrfmiddlewaretoken'。$('输入[名称=csrfmiddlewaretoken])VAL();
        CITY_NAME:$(#CITY_NAME)VAL()。
    },
    完成:函数(){
        //这里做什么
    }
});
 

了解更多关于CSRF和AJAX在Django的位置:的https:/ /docs.djangoproject.com/en/dev/ref/contrib/csrf/#ajax

希望它帮助。

I am not able to get the input text field data with id city_name from the form via jQuery-Ajax method.

The error that I keeps getting is "NetworkError: 403 FORBIDDEN - http://127.0.0.1:8000/dashboard".

I know how to get the data using hidden field type, but that option cannot be used here and moreover, getting data from hidden field is now an outdated method. So, how can I get data using Django-AjaxJquery method.

HTML

<form type = "POST">
            {% csrf_token %}    
            <input type="text" placeholder = "Enter City Name" id = "city_name">
            <input type="button" value = "On" id="on">
            <input type="button" value = "Off" id="off">
</form>

JS File

$(function(){

    $("#on").click(function (event) {
        event.preventDefault();

        $.ajax({
            type: "POST",
            url: "dashboard",
            data : {
                'city_name' : $("#city_name").val(),
                   },

               });
    });
});

View.py

@login_required
def dashboard(request):
    ctx = {}

    if request.is_ajax():
        city_name = request.POST['city_name']
        print city_name

    return render_to_response('dashboard/dashboard.html',ctx, context_instance = RequestContext(request))

urls.py

urlpatterns = patterns('',
    url(r'^dashboard$','apps.dashboard.views.dashboard', name = 'llumpps_dashboard'),

    )

解决方案

It is missing the CSRF token in the request.

You can either use @csrf_exempt decorator for your view like:

@login_required
@csrf_exempt
def dashboard(request):
    ...

Or send the token along with the request:

$.ajax({
    type: "POST",
    url: "dashboard",
    data : {
        'csrfmiddlewaretoken': $('input[name="csrfmiddlewaretoken"]').val();
        'city_name' : $("#city_name").val(),
    },
    complete: function () {
        // do whatever here
    }
});

Read more about CSRF and AJAX in Django here: https://docs.djangoproject.com/en/dev/ref/contrib/csrf/#ajax

Hope it helps.

这篇关于Django的阿贾克斯,jQuery的不获取数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆