Flask application experiencing JavaScript issues on local host, yet functioning properly when accessed via 127.0.0.1

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}]"

Answer №1

To run your app, always be sure to use app.run(host= '0.0.0.0') instead of just app.run()

Answer №2

During my troubleshooting process, I included alert() statements at various points in the javascript code. It became evident that the python REST application hosted on Heroku was causing delays, leading to timeouts in the asynchronous ajax calls.

As a temporary solution, I have added some delays. However, a more efficient approach would be to implement the method described in the following link: Need a delay function javascript

Answer №3

If you are experiencing difficulties after updating your Mac to macOS Monterey, it may be due to the new Airplay feature using port 5000.

You can verify this by running the following command:

lsof -i:5000

To resolve this issue, disable the “AirPlay Receiver” in the “Sharing” System Preferences as Control Center stops listening on those ports when this option is turned off.

For more information, please visit the link below: https://developer.apple.com/forums/thread/682332

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

How to selectively display specific columns when outputting JSON to a dynamic HTML table?

I'm looking to output specific JSON data in an HTML table with JavaScript. The headers will remain the same, but the JSON content will vary. Currently, I have a working function that generates the entire table using JavaScript: <body> ...

Tips on showcasing array elements within a table's tbody section and including several values within the same array

I am struggling to figure out how to display array values in my table rows that I receive from input values. I have created an array, but I can't seem to find a way to display them in the table. Furthermore, I want to be able to add more values to th ...

Invoke a function from a popup window, then proceed to close the popup window and refresh the parent page

When a link in the parent window is clicked, it opens a child window. Now, when the save button is clicked in the child window, I need to trigger a Struts action, close the child window, and reload the parent window. function closeChildWindow(){ document. ...

When working with NextJs, you may encounter a ValidationError indicating that the configuration object is invalid. This error occurs when Webpack has been initialized with a configuration object that doesn't

After upgrading from Next v12 to v12.2.3, I encountered a problem when running "yarn dev" with a new middleware.js file in the root directory: ValidationError: Invalid configuration object. Webpack initialization error due to mismatched API schema. - Deta ...

Is there any way to remove the two default aspNetHidden Divs in asp.net web forms?

After creating an empty webform page in asp.net, the generated code looks like this: <%@ Page Language="C#" AutoEventWireup="true" CodeBehind="WebForm1.aspx.cs" Inherits="Threetier.WebForm1" %> <!DOCTYPE html> <html xmlns="http://www.w3.org ...

React does not play well with the Sendgrid Node.js library

Seeking assistance with integrating node.js to handle email sending on my website. Interested in having the email form immediately send upon submission, rather than using the standard "mailto" action. Utilizing Sendgrid as the email service for API and ser ...

Pause and check for the completion of data loading in mapstate

I have a stored userProfile in the Vuex state in order to access it throughout my project. However, when I try to use it in the created() hook, the profile is not loaded yet during the initial page load. Although the object exists, it does not contain any ...

What is the best way to implement rate limiting for Next.js server actions?

In my current project, I have implemented server actions in next.js following the guidelines provided on Server Actions Although everything is functioning properly, I am now looking to add rate limiting to the server action to prevent potential spam or at ...

Displaying JSON data in an HTML table cell format

Hey everyone, I need some help with the following task: I am working on displaying a list of log lines in an HTML table. Some of these lines will contain JSON strings, and I want to format the JSON data within the table when the HTML file is loaded from ...

Making an API request using jQuery

I am currently working on creating a .js file that will send data to an external API, wait for a response, and then interpret the results. The external API I am using is XML-based and requires an HTTPS Post request with the XML content in the body (content ...

Vue.js having compatibility issues with Semantic UI dropdown feature

I have recently started exploring Vue.js and I must say, I really enjoy using Semantic UI for my projects. In Semantic UI, dropdowns need to be initialized using the dropdown() function in semantic.js. This function generates a visually appealing HTML str ...

Steps to include a Target property in a freshly created MouseEvent

Trying to dispatch a contextMenu event, I've noticed that in the MouseEvent interface for TypeScript, the target property is missing, even though it is documented in the contextMenu documentation. Here's my TypeScript snippet: const emulatedMou ...

What is the best way to specify a function parameter as a Function type in TypeScript?

I'm currently delving into the world of TypeScript and I am unsure about how to specify a function parameter as a function type. For instance, in this piece of code, I am passing a setState function through props to a child component. const SelectCity ...

What is the best way to extract values from this PHP script?

I recently delved into the d3 javascript library and successfully created a scatter plot chart that pulls random values from an array. Below is the code snippet: <!DOCTYPE html> <meta charset="utf-8"> <head> <script type="text/ja ...

Issue: React child must be a valid object - Runtime Error Detected

As I delve into the world of React, NextJs, and TypeScript, I stumbled upon a tutorial on creating a navbar inspired by the 'Strip' style menu. It has been quite a learning journey for me as a newbie in these technologies. After seeking help for ...

Here's a guide on how to package and send values in ReactJs bundles

I'm currently involved in a ReactJs project that does not rely on any API for data management. For bundling the React APP, we are using Webpack in the project. The challenge now is to make the React APP usable on any website by simply including the ...

Enhance your user interface by incorporating badges into specific elements using Bootstrap version 5.2

I am currently using the most recent version of Bootstrap v5.2 and Vue 3 (for learning purposes). While searching on Stackoverflow, I came across a similar question, but it was related to an older Bootstrap version. In my project, I have a select element ...

The onClick event cannot be triggered within a div that is generated dynamically

I am having an issue with a jquery code that dynamically generates a div. The problem I'm facing is that the onclick function for the anchor tag is not calling the required function. Below is the code snippet: $("#new").append(' <ul cla ...

Gulp does not work well with Android APK compilation

Greetings, I am facing an issue while trying to compile my Android app using gulp with the command: gulp --prod -p android. The problem arises when comparing the file size of the generated APK between myself and a colleague. When my colleague compiles, the ...

Angular2 fire fails because the namespace 'firebase' does not export the member 'Promise'

I recently set up Angular 2 Fire on my project. "angularfire2": "^5.0.0-rc.0", Now, in my root module (app module), I have the following setup: export const firebaseConfig = { apiKey: "mykey", authDomain: "....", databaseURL: "...", projectId: ...