如何发布使用jQuery /阿贾克斯在Django? [英] How do I POST with jQuery/Ajax in Django?

查看:232
本文介绍了如何发布使用jQuery /阿贾克斯在Django?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我尝试后使用jQuery / AJAX在Django的数据时遇到了问题。当我运行低于code,再点击测试按钮,整个页面重新加载,这不是我希望发生的(这就是为什么我使用AJAX)。我也无法证实Ajax请求也越来越为蟒蛇视图。有什么建议?

修改

我所做的编辑为返回FALSE,事件。preventDefault()。一个新的页面没有加载,但我仍然无法看到的DIV ID ='消息'字段更新的文字。我不知道如果数据是越来越发送。我看到在控制台:

  POST / edit_favorites / HTTP / 1.1403 2294
 

views.py

 从django.shortcuts进口render_to_response
从django.http进口的Htt presponse

高清edit_favorites(要求):
    如果request.is_ajax():
        消息=是的,AJAX!
    其他:
        消息=不阿贾克斯
    返回的Htt presponse(消息)
 

URL配置:

  URL(R'^ edit_favorites /','edit_favorites),
 

HTML

 <形式方法='后'ID ='测试'>
     <输入类型=隐藏值=video34/>
    <输入类型=提交值=测试按钮/>

    < D​​IV ID ='消息'>初始文字< / DIV>

< /形式GT;
 

的javascript:

 <脚本类型=文/ JavaScript的>
   $(文件)。就绪(函数(){
       $(#考)。递交(函数(事件){
       。事件preventDefault();
            $阿贾克斯({
                 键入:POST,
                 网址:/ edit_favorites /,
                 数据: {
                        从形式$('#测试)VAL()//:视频。
                        },
                 成功:函数(){
                     $('#消息)HTML(< H2>联系表提交<!/ H2>中)
                    }
            });
            返回false;
       });

    });
< / SCRIPT>
 

解决方案

错位返回false; 。它应该在 .submit()函数的结束。所以移动一行向上。

  $(文件)。就绪(函数(){
       $(#考)。递交(函数(事件){
            $阿贾克斯({
                 键入:POST,
                 网址:/ edit_favorites /,
                 数据: {
                        从形式$('#测试)VAL()//:视频。
                        },
                 成功:函数(){
                     $('#消息)HTML(< H2>联系表提交<!/ H2>中)
                 }
            });
            返回false; //< ----将在这里
       });

});
 

更新:

有关问题的 POST / edit_favorites / HTTP / 1.1403 2294 。检查这个职位看起来像你的问题,

Django的jQuery的POST请求

I am trying to POST data using jQuery/AJAX in Django and am having trouble. When I run the code below and click on the 'test' button, the entire page re-loads again, which isn't what I want to happen (that's why I'm using AJAX). I'm also not able to confirm the ajax request is getting to the python view. Any advice?

edit

I've made the edits for return false and event.preventDefault(). A new page doesn't load, but I'm still not able to see the updated text in the div id = 'message' field. I'm not sure if the data is getting sent. I'm seeing in the console:

POST /edit_favorites/ HTTP/1.1" 403 2294

views.py

from django.shortcuts import render_to_response
from django.http import HttpResponse

def edit_favorites(request):
    if request.is_ajax():
        message = "Yes, AJAX!"
    else:
        message = "Not Ajax"
    return HttpResponse(message)

urlconf:

url(r'^edit_favorites/', 'edit_favorites'),

html:

<form method='post' id ='test'>
     <input type="hidden" value="video34" />
    <input type='submit' value='Test button'/>

    <div id = 'message'>Initial text</div>

</form>

javascript:

    <script type="text/javascript">
   $(document).ready(function() {
       $("#test").submit(function(event){
       event.preventDefault();
            $.ajax({
                 type:"POST",
                 url:"/edit_favorites/",
                 data: {
                        'video': $('#test').val() // from form
                        },
                 success: function(){
                     $('#message').html("<h2>Contact Form Submitted!</h2>") 
                    }
            });
            return false;
       });

    });
</script>

解决方案

Misplaced return false;. It should be at the end of .submit() function. So move it one line upwards.

$(document).ready(function() {
       $("#test").submit(function(event){
            $.ajax({
                 type:"POST",
                 url:"/edit_favorites/",
                 data: {
                        'video': $('#test').val() // from form
                        },
                 success: function(){
                     $('#message').html("<h2>Contact Form Submitted!</h2>") 
                 }
            });
            return false; //<---- move it here
       });

});

UPDATE:

About the issue POST /edit_favorites/ HTTP/1.1" 403 2294. Check on this post which looks similar to your issue,

Django jQuery post request

这篇关于如何发布使用jQuery /阿贾克斯在Django?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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