Django Ajax将数据添加到POST

时间:2018-08-15 23:18:54

标签: javascript jquery ajax django django-forms

我正在使用ajax提交表单和一些其他数据。我可以在console.log中看到所有数据,但是我的视图失败了,它没有获取额外的数据。

page.html

    //AJAX get location function
    function get_location(form) {
            var data = form.serializeArray();
            data.push({
                       {% if form.instance.id %}id: {{form.instance.id}},{% endif %}
                       city    : itemCity,
                       state   : itemState,
                       country :itemCountry,
                       lat     : itemLat,
                       lng     : itemLng
                       });
            console.log(data);
            $.ajax({
                    url  : "{% url 'posts:ajax_location' %}",
                    type : 'POST',
                    data : data,
                    success : function(json) {
                            console.log('success call')
                            if (json['status'] = 'Update Post') {
                                    $(location).attr('href', json['url']);
                            }
                            ...

views.py

def ajaxSetPostLocation(request):
if request.method == 'POST':
    if request.is_ajax():
        city = request.POST.get('city', None)
        state = request.POST.get('state', None)
        country = request.POST.get('country', None)
        lng = request.POST.get('lng', None)
        lat = request.POST.get('lat', None)
        pk = request.POST.get('id', None)

        response_data = {}

是否有其他方法可以从数组中获取数据?

0 个答案:

没有答案