Nuxt Axios not connecting with proxy leading to CORS issues

Encountering the following CORS error:

Access to XMLHttpRequest at 'https://gw.bilinfo.net/listingapi/api/export' from origin 'http://localhost:3000' has been blocked by CORS policy: Response to preflight request doesn't pass access control check: No 'Access-Control-Allow-Origin' header is present on the requested resource.

The configuration in my Nuxt.config.js file is as follows:

  proxy: {
    '/listingapi/api/export/': {
      target: 'https://gw.bilinfo.net/',
      pathRewrite: { '^/listingapi/api/export/': '' },
      changeOrigin: true
    }
  },

  axios: {
    proxy: true,
    baseURL: 'http://localhost:3000', // Used as fallback if no runtime config is provided
    withCredentials: true,
    headers: {
      Accept: 'application/json',
      'Content-Type': 'application/json'
    }
  },

Below is how my data fetching component is structured:

  async fetch() {
    const data = await this.$axios.$get(
      'https://gw.bilinfo.net/listingapi/api/export',
      {
        credentials: true,
        auth: {
          username: 'XXXXXXX',
          password: 'XXXXXXX'
        }
      }
    )
    this.biler = data.Vehicles
  },

Although everything works fine upon refresh, navigating through different pages triggers a CORS error and causes the data to disappear. It seems like the proxy is not being utilized properly, but I am unable to pinpoint the issue.

Answer №1

When using a proxy, remember to make the call in the following way:

  async fetch() {
    const data = await this.$axios.$get(
      '/listingapi/api/export',
      {
        credentials: true,
        auth: {
          username: 'XXXXXXX',
          password: 'XXXXXXX'
        }
      }
    )
    this.biler = data.Vehicles
  }

The proxy pass code below indicates that if you want to access the API URL https://gw.bilinfo.net/Example, you should call it using /listingapi/api/export/Example

  proxy: {
    '/listingapi/api/export/': {
      target: 'https://gw.bilinfo.net/',
      pathRewrite: { '^/listingapi/api/export/': '' },
      changeOrigin: true
    }
  },

In essence, whatever you specify in pathRewrite will be replaced with the target URL. I hope this explanation helps!

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

Display current weather conditions with the Open Weather API (featuring weather icons)

Hello everyone, I need some help from the community. I am currently working on a weather app using the openweather API. However, I'm facing an issue with displaying the weather conditions icon for each city. I have stored every icon id in a new array ...

ERESOLVE encountered issues resolving the dependency tree for a project using .NET Core 3.1 and Vue.js framework

I encountered an issue while trying to build my application. The error message is as follows: npm ERR! code ERESOLVE npm ERR! ERESOLVE unable to resolve dependency tree npm ERR! npm ERR! While resolving: <a href="/cdn-cgi/l/email-protection" class ...

Best Practices for Structuring Front-End Data (such as Product Information) in Vue.js

How should static product information data be stored within a vue2 single page application? In the past, I have used an external JS file that contained a JSON with product attributes (such as name, weight, height, etc). I prefer not to load via AJAX beca ...

Obtaining a compressed file via a specified route in an express API and react interface

Feeling completely bewildered at this point. I've had some wins and losses, but can't seem to get this to work. Essentially, I'm creating a zip file stored in a folder structure based on uploadRequestIds - all good so far. Still new to Node, ...

The JSON data sent from the primary Electron process is arriving as undefined in the renderer

Currently delving into an Electron project to explore the technology. It's been a captivating and enjoyable experience so far as I work on creating a basic home controller for my IoT devices. However, I've encountered a minor issue. In my main.js ...

What is the solution to resolving the error message "Uncaught ReferenceError: Pusher is not defined" in Vue.js 2?

Whenever I try to launch my application, the console shows the following error: Uncaught ReferenceError: Pusher is not defined and Uncaught ReferenceError: App is not defined Despite running the following commands in my terminal: npm install and ...

The Vue route parameters are not recognized within the function type

Seeking assistance on extracting parameters from my route in a vue page, I have the following implementation: <script lang="ts"> import { defineComponent } from 'vue'; import { useRoute } from 'vue-router'; export ...

The Vuetify VSelect component displays a blank comment in the DOM instead of the expected rendering

Incorporating vuetify into my project has been a success overall. The components like v-file-input and v-text-field are functioning properly. However, I am encountering an issue with v-select as it is not showing up as expected. Instead, something unusual ...

`Failure to prompt an error following an unsuccessful post request in a node.js application using axios and express`

I'm currently facing an issue while trying to implement password change validation. The problem lies in not receiving the errorMessage from the server in case of an error. Although I've successfully managed to update the password and send back a ...

Unable to retrieve the Date Range Picker value in Vuejs

I am new to VueJS and I am having trouble retrieving the value of the selected date range. When I click the button, the console prints out a value of null. Can someone please assist me with this issue? Below is the code snippet: App.vue <template> ...

The difference between emitting and passing functions as props in Vue

Imagine having a versatile button component that is utilized in various other components. Instead of tying the child components to specific functionalities triggered by this button, you want to keep those logics flexible and customizable within each compon ...

Retrieve information from deeply nested JSON and showcase using Vue-Multiselect

My goal is to fetch data from the server and present it in Multiselect using nested JSON, which can be done through Vue-Multiselect. Once displayed, I should have the ability to add new tags as needed, essentially updating the data. While I can display o ...

Vuejs and its unique feature of nested and interconnected slots

Utilizing Vuejs, I have developed two components known as First and Second. The component First renders the component Second. Second contains a named slot. First includes a <template> to fit into the named slot of Second. Within the <template> ...

While making a promise, an error occurred: TypeError - Unable to access the property '0' of null

I encountered an issue when trying to assign data from a function. The error appears in the console ((in promise) TypeError: Cannot read property '0'), but the data still shows on my application. Here is the code: <template> ...

What causes the ignoring of config.proxy in axios requests while working on a webpack project?

My objective I am aiming to make a request using [email protected] with full efficiency through an http proxy (squid). My project is built on Vue and uses the webpack template. I initialized it with vue init webpack proxytest The challenge Despite ...

Issue with setting a cookie on a separate domain using Express and React

My backend is hosted on a server, such as backend.vercel.app, and my frontend is on another server, like frontend.vercel.app. When a user makes a request to the /login route, I set the cookie using the following code: const setCookie = (req, res, token) = ...

Encountering an issue while trying to create a Vue3 Vite application because of the assignment of a read-only property 'name

I am facing an issue with my Laravel app that utilizes Vite, Vue3, and Inertiajs. Previously, I was able to build it successfully. However, now I encounter the following error: Cannot assign to read only property 'name' of function '(userOp ...

Vue error: Uncaught promise rejection - RangeError: The computed value update has exceeded the maximum call stack size

My computed code snippet: computed: { display: { get() { return this.display }, set(newValue) { this.display = newValue } } }, Attempting to update the computed value from a function in ...

Configuring Visual Studio Publish settings for Single Page Applications deployed on Azure

I have set up my Azure App Service and downloaded the publish profile settings to use FTP for publishing my single page app to Azure. However, I am interested in configuring this process in Visual Studio similar to how one would publish a .Net application ...

Ways to retrieve the baseURL of an axios instance

This question is being posted to provide an easy solution for fellow developers who may be looking for an answer. //Suppose you have an axios instance declared in a module called api.js like this: var axios = require('axios'); var axiosInstance ...