"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

Struggling with integrating Bootstrap into a Vue.js application due to a compatibility problem with

I am facing difficulties importing Bootstrap features such as modal in a Vue component that uses installed packages like bootstrap, popper.js, and jquery. I am working with nuxtjs for this project. Unfortunately, when trying to import these features, I en ...

How come the values in my object remain inaccessible even after assigning values to it with navigator.geolocation.getCurrentPosition() and returning it?

After successfully assigning values to my pos object within the whereAmI function using navigator.geolocation.getCurrentPosition(), I can confirm that lat and lng are present inside the object. However, attempting to access these properties later on resu ...

Updating a React JS State using a Parameter

Is it feasible to develop a function that accepts a parameter (either a string of the state name or the actual state) and then assigns the state related to the parameter? SetState(x) { // Suppose x can be any state we have already defined (it sh ...

Achieving functionality with a fixed header

I am struggling with the scroll functionality and smooth transition in my code for a sticky header. The scroll doesn't work smoothly, especially when the fixed header is activated. The #top-nav-wrapper barely scrolls as expected: <script> $(doc ...

Typescript: Why Lines Are Not Rendering on Canvas When Using a For-Loop

What started out as a fun project to create a graphing utility quickly turned into a serious endeavor... My goal was simple - to create a line graph. Despite my efforts, attempting to use a for-loop in my TypeScript project resulted in no output. In the ...

The browser fails to render an EJS-generated page when responding to a Fetch request

Today, I encountered a strange issue that has left me puzzled. In summary, I have been sending a PUT request from a user form to the backend. Since forms do not natively support PUT/DELETE requests, I had to use a script to handle this task for me. let for ...

The Angular filter received an undefined value as the second parameter

Currently, I am facing an issue while trying to set up a search feature with a custom filter. It appears that the second parameter being sent to the filter is coming through as undefined. The objects being searched in this scenario are books, each with a g ...

My goal is to populate all of these elements with Javascript

My goal is to have all the boxes filled with "excellent": https://i.sstatic.net/oHW6W.png let a = document.querySelectorAll('.select2-choice'); a.forEach((e) => {console.log(e)}) a.forEach((e) => {e.innerHTML = ` <span ...

Is it just me or does Img never work, no matter what the "src" is?

I am implementing Google's React example of a "Complex Grid" (located here) to create a Card-like layout. This is how the code appears: return ( <div className={classes.root}> <Paper className={classes.paper} ...

Setting up event listeners from a string array (using PIXI.js)

Hey there! I've encountered a bit of an interesting challenge that could easily be resolved by duplicating the code, but where's the fun in that? This project is more of an experiment for me, just to prove that I can do it. However, the idea has ...

Tips for determining whether a value is present in an array or not

I'm trying to prevent duplicate values from being pushed into the selectedOwners array. In the code snippet below, the user selects an owner, and if that owner already exists in the selectedOwners array, I do not want to push it again. How can I imple ...

Results not showing up

After attempting to incorporate the scores into a table, I am facing an issue where they are not displaying. Can anyone offer assistance with this problem? I am currently hosting on GitHub Pages. File: https://raw.githubusercontent.com/Eternal-Network/Ete ...

Replace or update the current array of objects that align with the incoming array

I've been struggling to figure out how to find and replace an array that matches the existing one. The issue lies with using the some method. Here is the current array: let existingData = [{ id: 1, product: 'Soap', price: ' ...

Establish a connection between the Discord Bot and a different channel

I need help with my Discord bot that should redirect someone to a different channel when they mention certain trigger word(s). I feel like there might be a missing line or two of code that I need to add to make it work properly. bot.on("message", messag ...

What is the correct way to end this jQuery statement?

I've been working on this for about 6 hours now. I ran it through multiple Lint tools and various other online tests, but I just can't seem to get the statement below to close properly. There's a persistent error showing up on the last line ...

What is the significance of XmlHttpRequest's readyState being at 2 when receiving a 200 HTTP response

Currently, I am utilizing pure JavaScript (no jQuery) to send a file to the server. The PHP script on the server returns status code 200 upon completion, but the JavaScript is interpreting it as readyState == 2. The PHP code responds with status code 200: ...

Having trouble with understanding the usage of "this" in nodejs/js when using it after a callback function within setTimeout

It's quite peculiar. Here is the code snippet that I am having trouble with: var client = { init: function () { this.connect(); return this; }, connect: function () { var clientObj = this; this.socket = ...

A step-by-step guide to loading a .php file via Ajax using JQuery when an item is selected from a dropdown

I have successfully populated a dropdown list using PHP from a database on the page named admin.php. Now, I am utilizing JQuery with Ajax functionality to display information when a surname is clicked from the dropdown menu. The goal is to call employerP ...

Is there a way to customize the Master Page in asp.net to resolve the Bootstrap4 dropdown issue? I am encountering an error message that says "Uncaught TypeError: Bootstrap

Hello everyone, I recently encountered an issue while trying to use dropdown in my project after updating to bootstrap 4. It was working perfectly fine before the update, but now I'm getting an error in the JavaScript. The error that pops up when I tr ...

Having trouble accessing the value of a node in JavaScript, as it keeps returning as "null"

Experimenting with converting HTML elements into lists. The objective is to generate a list of all values in a table upon clicking the "page next" buttons on it. Afterward, an alert should display the value of the first item in the list, which corresponds ...