I am working on a Django app and I’m having an issue with ajax and jQuery.
On my page, I have a javascript function that is called when a button is clicked. The purpose of this function is to make an ajax
call to a django view method. In this method some logic is done using some POST data which was sent with the AJAX call, and if the operation was successful, I want to return to
another page, otherwise, I simply want the user to remain on the same page, and a notice will be presented to the user using javascript.
below is a snippet of code:
(Javascript)
function makeCall() {
$.post(url, data, function(data) {
if(data=="ERROR") {
alert("THERE WAS AN ERROR");
} else {
//send user to a new page.
}
});
}
(Django-Python)
def viewMethod(request):
//LOGIC... Renders page using POST data.
if success:
//... preparing the response.
return HttpResponse(t.render(c))
else:
return HttpResponse("ERROR", content_type="text/plain")
How can I present the response page to the user using javascript/jQuery in the case that the operation is a success?
Thanks
In the viewMethod, you can render page using POST data but not the whole page, that is ,the page you will respond just has the body tags not the head.
and in front-end
if you still use the whole page, its formal like this: , you can use window.open or iframe tag to handle it.
But I advise just respond some snippets of html.