Django的阿贾克斯jQuery的电话 [英] Django Ajax Jquery Call

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

问题描述

这可能是基本的,但我已经花了两天时间,阅读了无数的教程,我仍然不能得到这个工作。为了简单起见我试图完成一项基本任务只是为了看看它的工作。我想送,从AJAX请求我捐视图。我看到,它成功地通过,但我期待我的模板也将得到更新,以真,但它仍然是假。任何帮助或建议,我AP preciate。

我的jQuery ...

  $。阿贾克斯({
    键入:POST,
    网址:/捐献/,
    数据: {
    测试:成功,
    },
    成功:函数(){
      警报(测试)
     },
    错误:函数(){
        警报(错误);
});
 

这是我的看法......

 高清捐赠(要求):
    如果request.is_ajax():
        测试=TRUE

    如果request.method =='POST':
        表= DonateForm(request.POST)
        如果form.is_valid():
            form.save()
    其他:
        表= DonateForm()
        测试=FALSE

    返回render_to_response('donate_form.html',{形式:形式,测试:测试},context_instance = RequestContext的(要求))
 

我的模板包括这...

 < H1> {{测试}}< / H1>
 

更新/解决方案

就像提到在这个问题上的意见,我没有做任何事情返回的数据。我更新了我的成功调用以下和它的工作

  $。阿贾克斯({
    键入:POST,
    网址:/捐献/,
    数据: {
    '拉链':拉链,
    },
    成功:功能(数据){

        结果= $(数据).find('#结果)。HTML()
        $(#id_date)replaceWith(&其中;跨度>中的结果+ +&所述; /跨度>中)。

    },
    错误:函数(){
        警报(错误);
    },
 

解决方案

我认为这个问题是在您传递数据。你使用萤火虫?一个极好的工具,如果你传递任何的 POST检查,这是一个很好的工具,Web开发一般。

下面是一个工作的例子从一个表单发送Ajax调用

  $(#的形式)。递交(函数(事件){
        变量$形式= $(本),
        $输入= $ form.find(输入,选择按钮,文本域),
        serializedData = $ form.serialize();

        $阿贾克斯({
            网址:/捐献/,
            类型:后,
            数据:serializeData,
            成功:函数(响应){
                警报(响应)
            }
        })
        。事件preventDefault();
    });
 

,然后你的观点可以是这个样子

 如果request.is_ajax()或request.method =='POST':
        表= DonateForm(数据= request.POST)
        如果form.is_valid():
            返回的Htt presponse(成功)
        其他:
            返回的Htt presponse(失败)
 

顺便说一句,除非你真的需要从 $所有的临时演员。阿贾克斯()我会建议你使用 $。员额(),而不是因为它似乎更直截了当地使用。

This may be basic, but I've spent two days, read countless tutorials and I still can not get this to work. For simplicitly I tried to accomplish a basic task just to see it work. I want to send make an ajax call to my donate view. I see that it successfully passes through but I was expecting that my template would also get updated to "TRUE" but it remains "FALSE". Any help or suggestions I appreciate.

my jquery...

$.ajax({
    type: "POST",
    url:"/donate/",
    data: {
    'test': 'success',
    },
    success: function(){
      alert('test')
     },
    error: function(){
        alert("Error");
});

this is my view...

def donate(request):
    if request.is_ajax():
        test = "TRUE"

    if request.method == 'POST':
        form = DonateForm(request.POST)
        if form.is_valid():
            form.save()
    else:
        form = DonateForm()
        test = "FALSE"

    return render_to_response('donate_form.html', {'form':form,'test':test}, context_instance=RequestContext(request))

my template include this...

<h1>{{ test }}</h1>

Update/Solution

Like mentioned in the comments on this question, I was not doing anything the the returned data. I updated my success call to the following and it worked

    $.ajax({
    type: "POST",
    url:"/donate/",
    data: {
    'zip': zip,
    },
    success: function(data){            

        results = $(data).find('#results').html()               
        $("#id_date").replaceWith("<span>" + results + "</span >");

    },
    error: function(){
        alert("Error");
    },

解决方案

I think the problem is at where you pass the data. Do you use Firebug? An excellent tool for checking if you pass anything in POST, it is an excellent tool for web development in general.

Here's a working example for sending Ajax call from a form

$("#form").submit(function(event) {
        var $form = $(this),
        $inputs = $form.find("input, select, button, textarea"),
        serializedData = $form.serialize();

        $.ajax({
            url: "/donate/",
            type: "post",
            data: serializeData,
            success: function(response) {
                alert(response)
            }
        })
        event.preventDefault();
    });

and then your view can look something like this

if request.is_ajax() or request.method == 'POST':
        form = DonateForm(data=request.POST)
        if form.is_valid():
            return HttpResponse("Success")
        else:
            return HttpResponse("Fail")

Btw, unless you really need all the extras from $.ajax() I would suggest you to use $.post() instead as it seems more straight forward to use.

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

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