Is remote resource sharing policy in vue.js restricted to backend access only?

Is there a way to enable CORS access in a project built with vue-cli and without node.js or express.js? I attempted to solve this by adding code to the vue.config.js file:

vue.config.js:

module.exports = {
  devServer: {
    proxy: {
      '/api': {
        target: 'http://18.700.121.3:8000/',
        ws: true,
        changeOrigin: true
      }
    }
  }
}

Vue template

import axios from 'axios';
export default {
  name: "Jokes",
  data () {
    return {
      songs: null,
    }
  },
  mounted () {
    const config = { headers: {'Access-Control-Allow-Origin': '*'}};
    axios.get('http://2.788.121.2:4000/', config)
      .then(response => (this.songs = response.data))
  }
}

Unfortunately, these attempts did not resolve the issue. I also tried using the Chrome plugin Access-Control-Allow-Origin, allowing access to localhost:8080, but it still didn't work. It seems that installing node.js and adding a header such as res.header("Access-Control-Allow-Headers","*") may be the only solution.

Answer №1

You can solve the issue by deleting the

const config = {headers: {'Access-Control-Allow-Origin': '*'}};
header from the request.

Having that extra header will only serve to confound the browser.

Answer №2

One method is to utilize the Cors Plugin in Chrome, such as Moesif CORS, or downgrade your Chrome version to below 71.

Another approach is to adjust the server-side code, especially if using Express.

By incorporating the following code snippet, you can successfully resolve this issue:

var cors = require('cors')

var app = express()
app.use(cors())

Answer №3

  1. First step: Shut down Chrome.
  2. Next, press the Windows Key and then tap R on your keyboard to open the "RUN" dialog box.

  3. Type in

    chrome --disable-web-security --user-data-dir
    into the input field and execute it.

  4. Finally, launch your vue.js application.

I hope this solution works for you.

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

Script function in Google Sheets HTML not being called

Within my Google app, I have the following HTML code that is supposed to call a function below. However, I am not getting any response. This script has been used consistently throughout my code until now. <div id= "right_column"> <p> ...

Encountering a MySQL Server error while attempting to insert exclusive records using the ROWCOUNT function

Currently, I am experimenting with some JavaScript code in Node.js, utilizing the MySQL package within Node and running MySQL queries in the Visual Studio Code terminal. Despite trying various approaches such as adding (), '', "", etc., isolatin ...

abandoning the upload of an item into a THREE.js environment

Currently, I am working on a THREE.js scene where I need to prevent uploading multiple files into the scene simultaneously. The setup involves using Angular to implement the three js scene and canvas as a factory to maintain only one instance of a canvas a ...

Only reveal a single div when hovering

I am currently working on a project that involves utilizing the Wikipedia API to allow users to search for and retrieve information from Wikipedia. I have made significant progress, but I have encountered an issue. When hovering over the "each-list" div, t ...

Access data in an array using a specific index in Typescript

Here is the JSON format I am working with: { "records": [ { "ID": "4", "TYPE": "landscape", "FIMAGE": "viewveni.jpg", "COUNT": "4444" } ], "pagination": { "count": 1, "page": 1, "limit": 10, "totalpages": 1 } } I'm facing a challenge trying to retri ...

A step-by-step guide on setting up flow types for @material-ui/core version 4

Is there a way to install flow types for material-ui/core version 4.x.x? It seems like the last update was for version 1.x.x here. The documentation on this topic is quite limited here. I'm unsure if there is still support for this, especially since t ...

Guide on connecting 2 elements using the jQuery click event

When it comes to closing a modal with the class submitmodal, I have found success using this code: $('.submitmodal').click(function() { window.location.hash='close'; }); To handle clicks on the body, I use the followin ...

An Array of objects is considered NULL if it does not contain any values before being iterated through

Working with Files in TypeScript (Angular 8) has led me to Encode the files in Base64 using the code below: private async convertToBase64(evidences: Array<EvidenceToDisplay>): Promise<Array<EvidenceToDownload>> { const results: Arr ...

Ways to monitor and manage the most recent asynchronous request

My AngularJS controller is handling asynchronous calls to a service that returns promises. $myService.getData(ctrl.state) .then(function(data) { updateInterface(data); }); Issue arises when the promises return out of order, resulting in interface dis ...

Experience a unique issue with Google Maps API where the map appears greyed out upon loading for the first time, and the specific

I'm facing a strange issue with Google Maps. When the map loads, all I see is a grey box with tools displayed, but no icons appear on the bottom-right corner - only white boxes. If I move the map to the left, the left areas load properly; however, mo ...

Mastering the art of utilizing Context API and Provider for optimal functionality

Currently, I am working on implementing a dark mode feature in my project. Despite everything appearing to be set up correctly, it seems like the Context at App is not re-rendering when I use the setTheme function inside the Provider. Can someone help me t ...

When an input value changes in jQuery, the script attempts to locate the parent element and then find the next input. However, in some cases, the value returned

I am currently attempting to retrieve the nearest input field within the same or different class that is located in the subsequent row div section. Unfortunately, I am receiving an 'undefined' response and unable to acquire the desired informatio ...

Extract information from a database table for presentation as simple text

I am looking to extract information from each row and display it as plain text on the same page within a paragraph. Here is an example table for reference: <table> <thead> <tr> <th class="a header">A</th ...

The absence of the window object is causing issues with Vue.js and Vuetify when using

Encountering issues when trying to add/integrate Vuetify, such as importing it like import Vuetify from 'vuetify/lib' and using Vue.use(Vuetify) in the main.js file. Main.js import Vuetify from 'vuetify/lib' Vue.use(Vuetify) ...

Vuetify - custom filter for advanced datatable restrictions

Currently, I am faced with a challenge in filtering data from a table where the object names are similar and case sensitive, such as "A", "Aa", or "a". I am struggling to filter the data by these exact values when using a v-select bound to the search funct ...

Leveraging XMLHttpRequest in Mozilla Extension creation

I'm encountering some issues when trying to call a PHP file using Ajax in my Mozilla Extension. Both the JavaScript (Ajax) and PHP files are located in the directory /myextension/content. Here is the function I use to make the Ajax call: function aj ...

Customizing JQuery Ajax Get

Is there a way to map incoming data to a specific data type instead of the default generic object when using one of the many options available at http://api.jquery.com/jQuery.ajax/? I typically convert the generated object by taking each property and putti ...

` `endless scrolling within reactjs` `

function InfiScroll ({items}) { items.filter(newItem => !existingItems.some(existingItem => existingItem.key === newItem.key)).map(newItem => <Item item={newItem} key={newItem.key}></Item>) } I am working with a component ...

Issue with VueJS navbar unexpected animation

I created a fixed navbar with a fade-out transition on the <router-view>. However, I noticed that when the view transitions in, the navbar also has an unwanted fade-out effect. Why is an element outside the <transition> wrapper being affected b ...

Directing communication in AngularJS: From controller to directive

I have a query about how a controller communicates with a directive created within that same controller. I am specifically interested in determining the recommended or best approach for this scenario. There are three possible approaches that come to mind: ...