My web application is built using Python with Flask for the server, and HTML with JavaScript for the user interface that utilizes callback functions and POST methods.
While the application runs smoothly with redirections and REST API calls at: Upon clicking the Submit button, nothing occurs when accessed from: http://localhost:5000/ Similarly, there are no actions triggered when accessed from: <-- the HTML + JavaScript can be found here
It seems like the JavaScript functionality is not working properly when the application is running without the 127.0.0.1 address. However, the CSS and separate pages function as expected.
EDIT:
The following JavaScript scripts direct me to: Which is the same as:
This redirecting behavior does not work when deployed on Heroku.
<script type="text/javascript">
function Submit(callback) {
//url = 'http://finhelper.herokuapp.com/api';
url = '/api';
ItemJSON = '[{"Married": 0,"Education": 0,"ApplicantIncome": 5000,"CoapplicantIncome": 1100,"LoanAmount": 150,"Credit_History": 1}]';
var xhttp;
var l_redir ='/';
xhttp=new XMLHttpRequest();
xhttp.onreadystatechange = function() {
if (this.readyState == 4 && this.status == 200) {
myFunction(this);
}
};
xhttp.open("POST", url, true);
xhttp.setRequestHeader("Content-type", "application/json");
xhttp.send(ItemJSON);
window.location.href = l_redir; //not being used
return false;
}
function myFunction(xhttp) { //Callback function
retval = xhttp.responseText;
var redir = '-';
if (retval.includes("approved")) {
redir = "/approve";
} else {
redir = "/reject";
}
redirect(redir); //Final redirection depending upon ML Return value
}
function redirect(redir) {
window.location.href = redir;
return false;
}
</script>
2nd EDIT: Although I can access the POST API running on the same app.py file via CURL, it doesn't work when called from JavaScript. Hence, no HTTP response is received, which was identified using an alert(). Any reasons or suggestions?
curl http://finhelper.herokuapp.com/api -k -X POST -H "Content-Type: application/json" -d "[{\"Married\": 0,\"Education\": 0,\"ApplicantIncome\": 5000,\"CoapplicantIncome\": 1100,\"LoanAmount\": 150,\"Credit_History\": 1}]"