0

I used below function to send my form data with ajax:

$(function() {
        $('#sendButton').click(function(e) {
            e.preventDefault();
            var temp = $("#backupSubmit").serialize();
            $.ajax({
                type: "POST",
                data: temp,
                url: 'backup/',
                success: function(data) {
                     $("#response").html('<p>{% trans "you can download it from here:" %}'+'<a href="'+data+'">{% trans "download" %}</a></p>');
                        $("#response").show();
                }
            });
        });
    });

now i want to use this function:

$("#sendButton").click(function(e){
        e.preventDefault();
        save_data();
        if(save_data()) {
            alert('Saved!');
        } else {
            alert('Failed!');
        }
    });
function save_data() {
        $.post("backup/", {
                    csrfmiddlewaretoken: $("input[name='id_csrfmiddlewaretoken").val(),
                    ???? backup_from: $("#backupSubmit").serialize()
                },
                function(data) {
                     data = json_parse(data);
                    if(data.status=="success") {
                        return true;
                    } else {
                        console.log("status: "+data.status)
                        console.log("error: "+data.error)
                        console.log("POST DATA: "+data.data)
                        $.each(data.data, function(i, n){
                            console.log(">"+i+": "+n);
                        });
                        return false;
                    }
                },
                "json"
        );
    }

but in view, it seems that form is not valid! and this is my form:

BACKUP_CHOICES = (('systemSettings',_("systemSettings")),
                  ('ruleSet',_("ruleSet")))

class backupForm(forms.Form):
    backup_from = forms.MultipleChoiceField(widget=CheckboxSelectMultiple, label=_("backup from"), required=True,
        choices=BACKUP_CHOICES, error_messages={'required': _("Please choose at least one option")})

It sends data with post, and i don't have any validation check, except requirement. i don't know why the form is not valid? Is it correct to send form data like the above (next to ????)? thanks for your help:

1 Answer 1

1
  1. Just send the post request with data = $("#theForm").serialize(), it should include the csrftoken etc ...
  2. Use firebug or webkit inspector (press F12) to monitor the post requests made by the website.
  3. If the server responds with an error, actually do read the traceback in the response using the inspector.
  4. If the server doesn't respond with an error, do read the response in the inspector too.

it seems that form is not valid

"We can't stop here, this is bat land." Is it valid or not ? If it's not valid then the view should respond appropriately, and the javascript should be able to use that response to inform the user that the form is not valid.

In case you need to trace the Python code, put in your view import pdb; pdb.set_trace() so that you can debug it properly and see if it's valid or not and also what is it doing.

Also, you didn't post your view code. Anyway, I think that point 1. should work.

Sign up to request clarification or add additional context in comments.

2 Comments

thank you. some thing i forgot to say is that it works with first function; but with the second it doesn't. so i guess may be i should send form data with an other way. although the POST request seems to be true. (is the same as first one), but form is not valid in second one!
Yes, the second code is not sending the same data, see point #1 of this answer ........

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.