1

.ajax() can send a post request and get data in return where as .load() can get any element in the rendered page. How to create a form when submitted(asynchromously) instead of getting back some data should get the page element of the rendered page that would be generated had there been normal submission instead of ajax submission?

I dont want to write views(Django) for xhr, normal requests separately. So, When I submit a form by ajax I dont want to hijack default action but only want to get some element of the rendered post submission page instead of actually being redirected to that post submission page which would have happened hadn't it been an xhr request.

0

1 Answer 1

2

Update:

load will do a POST rather than a GET if you supply the data to send as an object rather than a string. From the docs:

Request Method

The POST method is used if data is provided as an object; otherwise, GET is assumed.

So:

$("#target").load("/path/to/resource selector_for_relevant_elements", {});

..should convert the load from GET to POST. Of course, you'd replace {} with the arguments you want to send.


Original answer:

You can do the POST directly with ajax and then process the returned HTML yourself. For instance, to turn this load:

$("#target").load("/path/to/resource selector_for_relevant_elements");

..into a POST:

$.ajax({
    url: "/path/to/resource",
    method: "POST",
    dataType: "html",
    success: function(html) {
        // Build the elemnts of the result in a disconnected document
        var page = $("<div>").append(html); // See note below

        // Find the relevant elements and put them in target
        $("#target").html(page.find("selector_for_relevant_elements"));
    }
});

I've done the wrapper div because that's what jQuery's load function does. You may want to look at the source for load (that line number will rot, of course, but the filename is unlikely to change) to see if there are other tricks you need to replicate.

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

3 Comments

Thanks.That solves one issue, other one being how to let the submit button do its default action and get the elements of rendered page into the existing page without going to that page.
Seems like that's not possible with js.
@KamalReddy: If you allow the default action, your page will be replaced by the results. But it's easy to create an object with the form's values. Start with a blank object {}, then loop through the form fields in document order, skipping any disabled ones, and add props to the object using the name of each field and its value. Only include checkboxes or radio buttons if they're checked. If you see a field name you've seen before (so the property is already set), turn the property value into an array and push the new value on the array. file fields are the only real problem.

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.