I have written a view which responds to ajax requests from browser. It’s written like so –
@login_required
def no_response(request):
params = request.has_key("params")
if params:
# do processing
var = RequestContext(request, {vars})
return render_to_response('some_template.html', var)
else: #some error
# I want to send an empty string so that the
# client-side javascript can display some error string.
return render_to_response("") #this throws an error without a template.
How do i do it?
Here’s how I handle the server response on client-side –
$.ajax
({
type : "GET",
url : url_sr,
dataType : "html",
cache : false,
success : function(response)
{
if(response)
$("#resp").html(response);
else
$("#resp").html("<div id='no'>No data</div>");
}
});
render_to_responseis a shortcut specifically for rendering a template. If you don’t want to do that, just return an emptyHttpResponse:However, in this circumstance I wouldn’t do that – you’re signalling to the AJAX that there was an error, so you should return an error response, possibly code 400 – which you can do by using
HttpResponseBadRequestinstead.