I am using Jquery colorbox and flask. When the user hits an icon the script submits a url to render the form inside the colorbox. When the user hits the save button the form is submitted and the colorbox closes. The problem is I just want the box to close, and not reload the screen, although that works fine there is no reason to redraw it. The thing I do not understand is how you return nothing, or do nothing in the view that causes the screen to reload. Here is the view code:
@listings.route('/notes/<string:find>',methods=['GET','POST'])
def notes( find = None ):
""" Ajax call to handle notes
"""
find = Found.objects.get( pk = find )
if request.method == 'GET':
return render_template('note.html', find = find )
if 'save' in request.form:
find.notes = request.form['note']
find.save()
#return redirect( url_for('listings.landing', search=find.search.pk))
return '',200
The redirect reloads the screen and the return '',200 cause a blank screen. How do I tell flask to do nothing on return?
The issue is not Flask ... the "issue" is the browser (and indirectly, the HTTP protocol). If you return a 30X
level response the browser issues a new request for the redirect URL transparently ... and in the case of a 200
response the browser will display what it received to the end user (in most cases). This is because a redirect
says "the thing you are looking for is actually found at this other address" and a 200 OK
says "you found what you are looking for, and here it is".
You will want to return a 204 No Content
rather than a 200 OK
:
return '', 204
A 204
response says to the consuming entity, "nothing new here, I did what I needed to and I don't have anything to say to you about it". In the browser, this results in the page from which the request was made staying on the screen.
Thanks it worked I just wanted to share what I did to finally get it work. I had to use this
.on('click', '#colorbox .cancel, #colorbox .note', function(e) {
if ( $(this).attr('class') == 'note' )
$('.notes').submit();
$.colorbox.close();
e.preventDefault();
})
and the html looks like this
<form action="{{url_for('listings.notes', find=find.pk)}}" method='post', class='notes'>
<textarea rows="4" cols="45" name='note'>{{find.notes|safe}}</textarea>
<div class="bottom">
<a href='#' class="cancel">cancel</a>
<input type='submit' name='save' value='save' class='note'/>
</div>
</form>