shogun656 shogun656 - 3 months ago 21
Apache Configuration Question

Using JQuery to get JSON from Flask is returning null sometimes

So I am developing on oracle Linux Server release 6.6 and I recently switched over to Apache 2.4 for my web server. I am developing a Flask web service so I was using Flask's local WSGI server to do all my debugging.

In the application, i have a form and when you click the submit button, it does a JQuery $.getJSON() call to my Flask backend. Then in the backend, I return JSON data and alert the user of the data. The problem is that most of the the time it returns null and doesn't return my data. Other times it returns my data and everything is fine. This worked perfectly when I was using the local WSGI server. It was once I migrated to Apache when this error started happening so I believe the problem is from Apache.

Also I print out my json data before returning so I know for sure it is not returning null data but I still get this error.

My HTML/JavaScript code

<div id="restore_submit" class="restore_submit" style="display:none;">
<form onsubmit="restoreDB()">
Type the database ID and Snapshot ID of the database you want to restore
<br><br>
RDS ID:<br>
<input type="text" id="DbName2" name="DbName">
<br>
Snapshot ID:<br>
<input type="text" id="snapshot_id" name="snapshot_id">
<br><br>
<input type="submit" value="Restore">
</form>
</div>

function restoreDB() {
var DbName = document.getElementById('DbName2').value;
var snapshot_ID = document.getElementById('snapshot_id').value;
var check = confirm('Are you sure you want to restore ' + DbName);
if(check){
$.getJSON('/restore_rds', {
Dbname: DbName,
snapshot_id: snapshot_ID
}, function(data) {
if(data){
if(data && data.error){
alert(DbName + " is being restored using this snapshot_id " + snapshot_ID);
}
else{
alert(data.error);
}
}
else{
alert("returned null");
alert(data);
}
});
}
}


My Python code

#This is for the restore script
@app.route('/restore_rds')
#@login_required

def restore_rds():
DbName = request.args.get('Dbname')
snapshot_id = request.args.get('snapshot_id')
error = rds_action.restore_db(DbName, snapshot_id, rds)
if error:
error = str(error)
else:
error = 'empty'
print error
print jsonify(error = error)
return jsonify(error = error)


EDIT:

So i fixed the issue after like a week of digging. Looks like my requests were getting cancelled before returning back to the webpage. So what i did was a e.preventDefault() in my JavaScript function.

so in my html i change it to

<form onsubmit="restoreDB(event)">


and in my JavaScript i added this

function restoreDB(e) {
e.preventDefault();


Now the webpage doesn't reload and it has enough time to wait for a response. Probably not the best fix but I am just happy I fixed it. If anyone recommends anything better, that will be great

Answer

So i fixed the issue after like a week of digging. Looks like my requests were getting cancelled before returning back to the webpage. So what i did was a e.preventDefault() in my JavaScript function.

so in my html i change it to

<form onsubmit="restoreDB(event)">

and in my JavaScript i added this

function restoreDB(e) { e.preventDefault();

Now the webpage doesn't reload and it has enough time to wait for a response. Probably not the best fix but I am just happy I fixed it. If anyone recommends anything better, that will be great

Comments