Suppose I have an AJAX function:
function callpage{
$.ajax({
method:"get",
url:"/abc/",
data:"x="+3
beforeSend:function() {},
success:function(html){
IF HTTPRESPONSE = "1" , ALERT SUCCESS!
}
});
return false;
}
}
When my “View” executes in Django, I want to return HttpResponse('1') or '0'.
How can I know if it was successful, and then make that alert?
The typical workflow is to have the server return a JSON object as text, and then interpret that object in the javascript. In your case you could return the text {“httpresponse”:1} from the server, or use the python json libary to generate that for you.
JQuery has a nice json-reader (I just read the docs, so there might be mistakes in my examples)
Javascript:
Django
An alternative View suggested by Issy (cute because it follows the DRY principle)