Issue with making requests across origins in Vuejs using axios

Currently, I am utilizing Vuejs and axios for handling web requests. So far, I have successfully managed to perform GET, POST, and PUT operations. However, the new challenge that I am facing is uploading media to the server. The server necessitates sending two-step requests in order to upload an image.

During the first step, I can acquire the upload URL link from the server within my promise and send the requisite headers. For the second step, all I need to do is use the obtained URL from step one and PUT the image file along with the same headers sent during the first step.

Unfortunately, I am encountering a Cross-Origin Request Blocked error and I am unsure of the root cause of this issue.

Below is the snippet of code:

 onPickFile() {
        var accessToken
        var self = this;

        firebase.auth().currentUser.getIdToken( /* forceRefresh */ true).then(function(idToken) {
            accessToken = idToken
            console.log("Id: " + self.id)
            console.log("Token : " + accessToken)
            var config = {
                headers: {
                    'Authorization': 'Bearer ' + accessToken,
                    'Content-Type': self.image.type,
                    'Media-Type': 'profile_image',
                    'x-goog-meta-media-type': 'profile_image',
                    'x-goog-meta-uid': self.id,
                }
            }
            console.log(config)

            axios.post('apilink', { // Step 1 - This step successfully returns the URL
                content_type: self.image.type
                }, config). // First step completed
            then(response => {
                console.log("Response URL is: " + response.data.upload_url)
                self.uploadUrlLink = response.data.upload_url

                console.log("Id: " + self.id)
                var config_2 = {
                    headers: {
                        'Authorization': 'Bearer ' + accessToken,
                        'Content-Type': self.image.type,
                        'Media-Type': 'profile_image',
                        'x-goog-meta-media-type': 'profile_image',
                        'x-goog-meta-uid': self.id,
                    }
                }
                console.log(config_2)
                axios.put(self.uploadUrlLink, self.image , config_2) // Step 2 - Issue with headers not showing up in "request headers"
                .then(response => { 
                    console.log("Response on PUT Image OK: " + response)
                })
                .catch(e => {
                    console.log("Error on PUT image: " + e)
                });

            })
            .catch(e => {
                console.log("Error on response on POST Image first step: " + e)
            });
        }).catch(function(error) {
            console.log("Error on getting token: " + error)
        });

    },

I am unable to view my headers under request headers: https://i.sstatic.net/OvEPr.png

Answer №1

When it comes to HTTP methods, using the OPTIONS method may not allow you to send data to the server. To overcome this issue, consider adding the OPTION method in the Access-Control-Allow-Methods header.

I trust this solution will be of assistance to 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

`In what way can I acquire auto-generated identifiers for selected documents?`

I am currently using Vue and Firestore in my project. Below is the code snippet I used to generate a document in the collection: <template> <input type="text" v-model="form.title"> </template> methods: { async sa ...

Ensure that the v-model in the function is reset in order for the v-tabs component to remain unhighlighted

Is there a way to reset the v-model when a specific value is selected? <template> <div id="app"> <v-app> <v-content> <v-tabs v-model="active_tab" slider-color="#F8940A" centered ...

Creating a Vue single-page application (SPA) and concealing the .vue files upon rendering in the

I have a Single Page App built using Vue.js and hosted on a node.js server. While the app is still in development, it will eventually be accessed by external customers. Due to the sensitive data involved, we want to prevent users from seeing the .vue files ...

Running a Node JS application concurrently with an Express API connected to MongoDB

Here's my current project plan: I've created a small Node app that fetches data about the stock market from an API and then stores the data in a Mongo DB (which is already up and running). My next step is to create an API that will allow other se ...

Change to a dark theme using React hooks in typescript

Hello, I am new to React and English is not my first language, so please excuse any mistakes. I have been trying to enable a dark mode feature on my website. Most examples I have found involve toggling between dark and light modes where you need to specify ...

disable full page scrolling on iOS devices

Can you achieve elastic scrolling for a single absolutely positioned div in Mobile Safari without causing the entire page to move up and down? Check out this basic example that illustrates the problem: <!doctype html> <html> <head> ...

Exploring Tabletop.js to retrieve information from an array of data

Issue I am currently attempting to retrieve data from an array that contains two objects. I have implemented Tabletop.js to fetch the data from a public Google Spreadsheet, but I encountered an error in the console stating ReferenceError: object is not de ...

Dynamic HTML DOM element name changes

One thing I'm considering is the process of dynamically changing element names in the DOM with JavaScript or jQuery when adding or removing child elements. For instance, if I decide to delete the second text box from a form that originally has three t ...

What causes the body onload = javascript function to run repeatedly?

Greetings for the absolute beginners out there, just dipping your toes in AJAX. I'm curious to know what exactly triggers the continuous change in divMessage content when the text "myName" is altered. 1) It appears that the Javascript function proce ...

Exploring the depths of Javascript scope with the Polymer starter-kit 2

When working on my-app.html ... <paper-icon-button icon="my-icons:menu" onclick="this.$._joesDrawerToggle()"></paper-icon-button> ... <script> Polymer({ is: 'my-app', ... _showPage404: function() { thi ...

Trigger modal on designated ID

My code includes a script that assigns a specific id to a button using a variable $i in a for loop: <button id='myBtn$i'>Open Modal</button>. This id should allow me to open specific modals with the corresponding $i value: <div id= ...

Experience the quick sort programming algorithm in action using Vue.js without the hassle of dealing with Promises

I am currently experimenting with the quicksort algorithm visualization using Vue.js for self-educational purposes. However, I am facing some difficulties with async/await implementation. Although array filling and shuffling seem to work fine (although I a ...

How to surround values and keys with double quotes using regular expressions in JavaScript

I am in need of a valid JSON format to request ES. I currently have a string that looks like this: { time: { from:now-60d, mode:quick, to:now } } However, when I attempt to use JSON.parse, I encounter an error because my ...

The script is showcasing text on both instances

I am currently working on a script to show text to users who are using adblock. The script I am using is as follows: ads.js <script>var canRunAds = true;</script> index.php <script data-rocketsrc="ads.js" type="text/rocketscript">< ...

Use JavaScript to retrieve a value and display it on a PHP page

I am trying to create a system that can generate and deliver JSON data. Here is the PHP code I have so far: <?php header("Content-Type:application/json"); require "data.php"; if(!empty($_GET['name'])) { $name=$_GET['name']; ...

There seems to be a problem with the external JavaScript file not functioning

After dragging and dropping the .js file into my ASP.NET project, I am facing an issue where it remains unresponsive, even though the code works fine when used inline. This problem is occurring while using VS 2017. Here is a snippet of my code: <scrip ...

How to Make a Div Tag Fade Effect with JavaScript in an External File

*** New Team Member Notice *** I'm currently working on an assignment and trying to implement a simple fade effect on a box. While it seems like this should be straightforward, I'm encountering some obstacles. Currently, the button causes the bo ...

Access the most recent state value with React's Context API

Trying out the React context API, Take a look at the someComponent function where I pass the click event (updateName function) to update the value of state.name from the GlobalProvider function. After updating state.name, it reflects in the browser but t ...

Exploring VueJS: Sorting objects within an array

Currently, I am tackling a challenge in vue-js. In my codebase, there exists a data object known as items. I am iterating through these items and aiming to present a dropdown menu containing the list of products. My goal now is to showcase only those pro ...

Can you explain the concept of binding and unbinding in jQuery?

Can you explain the concepts of binding and unbinding in jQuery in simple terms for someone who learns slowly? ...