"Utilizing Vue.js for frontend, this app utilizes axios from the client side, rather than

As a newcomer to the world of programming, I apologize if my question sounds basic. I currently have a setup where a Vue.js application is running on a Linux Red Hat server with Apache installed via XAMPP. Additionally, an ASP.NET Core 6 Web API application and MySQL database are also hosted on the same server.

The issue I'm encountering is that when I try to access my web app as a client from a different server, Axios seems to be looking for the Web API on my local machine instead of the localhost backend Linux server. Even if I specify the IP address of the Linux server in the Vue.js project, my laptop only has access to the Vue.js application, not the server. Can anyone help me figure out what's going wrong? Here's the Axios code used in my Vue.js project:

   sendDataToAPI(date, id) {
        const url = 'http://localhost:5256/api/job';
        axios({
            method: 'get',
            url: url,
            params: {
                date: date,
                id: id
            }
            })
        .then(response => {
        // Process the API response
        this.jobs = response.data;
        //console.log(response.data);
        })
        .catch(error => {
        // Handle any errors
        console.error('Error:', error.message);
        });
    }

Additionally, the "this.jobs" variable is used to populate a table within the same Vue.js project. However, when this method is triggered, Axios sends the GET request from the client side (my computer, phone, tablet, etc.) rather than consuming the Web API from the Linux localhost. All methods are running within the same mywebapp.vue file. Should I separate these methods into separate modules.js files? Despite being able to successfully receive responses from the Web API using curl on the Linux server itself and servers with access to the IP address, why is Axios behaving differently?

If anyone can shed some light on what might be going on or if I am missing something crucial, it would be greatly appreciated. My goal is to ensure that Axios requests are directed to the backend of my Linux server, not the client side.

Answer №1

When the URL 'http://localhost:5256/api/job' is used, Axios attempts to send the request to the localhost of the device running the JS Module. If you are accessing your app from a different device that has loaded the JS Modules from your XAMPP app, it will try to connect to the web API on that same device. The relative path (localhost) refers to the machine using the Axios-containing JS Module, which in this case is your browser.

To resolve this issue, you need to replace 'http://localhost:5256/api/job' with the IP address or hostname of your Linux server where the ASP.NET Core API is hosted.

PS: To break down the process:

  1. You access your website's frontend app.
  2. The app provides responses including js dependencies, css styles, and static files.
  3. Your browser now runs the code containing the axios call locally.
  4. By calling the relative Path "localhost", you are actually trying to reach your local client machine rather than the server.

That explains why it works when you are on the server, as the server's localhost refers to its own backend. However, the localhost from your client's machine is unaware of the server's backend.

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

Tips for passing a parameter to getters in vue

I've implemented a getter in my application to verify the stock availability and amount of products in the cart. Below is the code for my getters.js: export const checkStock = (state, productID) => { let stockAvailable = true; state.cart.fo ...

How to Handle 404 Errors for Specific Express Routes and Not Others

Struggling with the setup of a single page app using Angular routes on the front end, while facing backend issues. All database-related routes are functional, but any additional route designed to serve an HTML file or simple text like "hello world" result ...

Guide on navigating to a specific step within a wizard using Vue and TypeScript

In this wizard, there are 6 steps. The last step includes a button that redirects the user back to step 4 when clicked. The user must then complete steps 5 and 6 in order to finish the wizard. step6.ts <router-link to="/stepFour" ...

What is the best way to pause the display of dynamic search outcomes in React?

I am currently working on developing a dynamic search results workflow. While I have successfully managed to render the results without any issues, I am facing a challenge in toggling them off when all input is deleted from the search bar. When typing begi ...

Is the absolute positioned element on the left or right based on its current location?

Is there a way to assign absolute positioning with left at 0px or right at 0px depending on whether the positioned div will go outside of its container? For example, when you right click in a browser and see the menu appear to the right of where you click ...

Tips for using jQuery to slowly change a CSS property to "auto"

I've encountered a situation where I have an element styled with position:fixed, and bottom: auto;. When I apply the command .animate({bottom : '10%'});, it smoothly slides to the specified position. However, when I try to set it back to its ...

Component not being returned by function following form submission in React

After spending a few weeks diving into React, I decided to create a react app that presents a form. The goal is for the user to input information and generate a madlib sentence upon submission. However, I am facing an issue where the GenerateMadlib compone ...

The attempt to create the property 'and_ff' on the string 'and_chr 89' has failed

Encountering an issue with a Lambda function, I receive an error that does not occur when running the same code within an Express app. I'm puzzled. Data returned by caniuse.getLatestStableBrowsers(); [ 'and_chr 89', 'and_ff 86& ...

Laravel and Vue: Retrieve complex data with multiple relationships

In my data model, I have a concept of Exercise which is linked to Topic, and Topic is associated with Subject. By using the code snippet below: Exercise::with('topic')->get() I can easily access the properties of the current topic within Vu ...

Reboot JavaScript once a day for optimal performance

Is it possible for the JavaScript's ?random to be based on the current date so that it only loads once a day? <script type="text/javascript" src="http://external.example.com/bookmarklet.js?random"></script> I am asking this question beca ...

Experiencing issues with loading Vuex in Nativescript-Vue using the 'tns preview' command?

In the process of developing a trivia app for mobile, I have chosen to use Nativescript-Vue. When testing the app on the xcode iOS emulator, everything runs smoothly without any errors. However, when attempting to preview the app using the terminal command ...

Encountering a display issue within a port using Express

Recently, I enrolled in an advanced ExpressJS course. While exploring the course website, I stumbled upon the "hello world" section. Intrigued, I decided to copy and paste the code provided below: const express = require('express') const app = ex ...

Linking a Vue application within a PHP page using router hotlinking

Currently, I am in the process of developing a module for a CMS (Joomla) using VUE 3 with Router for the frontend. The prototype is functional and ready to be integrated into the CMS Module. The router is also working smoothly. However, when a user navigat ...

Tips for implementing pagination in a search result by adjusting the skip and limit values within the same query

While some may argue that this question belongs on programmers.stackexchange.com, after reviewing the Help Center of Stack Overflow, I believe it is a specific programming issue and will likely receive a better response here. I have developed a webapp usi ...

Tips on utilizing the useState hook for storing numerous key objects?

Currently, I am working with a candlestick chart for a cryptocurrency that displays data over different timeframes such as 1 minute and 30 minutes. Below is the code snippet that sets the initial state to show a 1-hour chart when a user first visits: const ...

Retrieve item from sessionStorage Cart

Greetings to the Stack Overflow community! I've often relied on Slack for valuable information and found solutions to many of my questions. However, this time, I'm facing a challenge that has me stumped. While I'm not an expert in JavaScript ...

What is the best way to automatically adjust the size of this HTML Menu to match the width of its

I am in the process of converting a Drupal 6 theme to Drupal 7 and I'm encountering some difficulties with this particular section. Below is the HTML code snippet: <ul id="nav" class=" scaling-active scaling-ready"> <li><a href="/demos ...

Is there a way to modify my code to eliminate the need for a script for each individual record?

Whenever I need to create a code with the ID by browsing through my records, is there a way to make just one function for all the records? $tbody .= '<script> $(document).ready(function(){ $("#img'.$idImage .'").click(functi ...

"Discover the magic of jQuery: Unveiling the hidden div with one simple CSS visibility change

I want to implement a functionality on my screen where the Previous button is hidden initially and only becomes visible when the user clicks the Next button. I have set the CSS property for the Previous button to hide it by default, but despite using an if ...

Is there a Quasar container that resembles Bootstrap?

The Quasar website claims that you won't need heavy libraries like Bootstrap when using their platform. They have it all covered with a small footprint internally. However, I am struggling to find how to replicate a Bootstrap-container-like behavio ...