Unless there's some peculiarity in Django, there should be nothing to prevent a POST request behaving differently from a GET request. Both should be capable of returning a URL string.
If you would prefer to receive a URL and replace the current document, then try the following :
function makeCall(url, data) {
function newPageError(jqXHR, textStatus, errorThrown) {
alert(textStatus + ' : ' + errorThrown);
}
$.post(url, data).success(function(data, textStatus, jqXHR) {
if(!data.match(/^http/)) {
newPageError(jqXHR, data, textStatus + ' - but error indicated');
} else {
location.href = data;
//location.replace(data);//alternative; gives different Back button behaviour
}
}).fail(newPageError);
}
Alternatively, if you would prefer to receive an HTML snippet and work within the current document, then try the following :
function makeCall(url, data) {
function newPageError(jqXHR, textStatus, errorThrown) {
alert(textStatus + ' : ' + errorThrown);
}
$.post(url, data).success(function(data, textStatus, jqXHR) {
if(data.toUpperCase() == 'ERROR') {
newPageError(jqXHR, 'ERROR', textStatus + ' - but error indicated');
} else {
$("#myDiv").html(data); //change jQuery selector to suit
}
}).fail(newPageError);
}
Notes:
- In both cases, two types of error are handled; an error indicated in a successful response, and a total HTTP error (eg internal server error).
- In both cases, it is assumed the url,
url
, and query parameters, data
, are passed to the function.
- In the first case, the server-side script would need to be modified to return a URL.