0

If you were to replace the code marked [from here - till here] with something minimalistic what would it be?

$('#post_form').submit(function(event){

            event.preventDefault();
            var data = {}

            // From here
           data.k = $('#k').val(); //'k' would be the id of some input field
            data.j = $('#j').val(); //and so on for 'l', 'm' , 'n' etc. you get it
            .......
            // Till here

            $.post("/ajax/post_test/", 
                data, 
                function(responseData) {
                  // handle the response
                },
                "json"

            );
        });

It would be awesome if this 'data' object could be directly used in Django as a form and hence be able to leverage Django's inbuilt validation system.

The ideal answer would look something like this -

data = $('#post_form').DumpData()

And if anybody feeling really kind today could you also please tell me how i can handle the response as well? I would be really really grateful. Thank you guys.

P.S. Save my soul.

1 Answer 1

1

You are looking for the serialize method:

The .serialize() method creates a text string in standard URL-encoded notation. It operates on a jQuery object representing a set of form elements.

So it doesn't give you an object, but it works as the data parameter for a jQuery AJAX call.

If you really want them as an object, there's serializeArray, which isn't exactly there as it is an array of objects with names and values, but you could then turn it into a one-level object.

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

2 Comments

Ahh and i fall in love with jQuery all over again. :]. Thanks buddy.
What thoughts about returning ajax validations through the django view?

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.