Utilize Vue client to call Google Vision API for image recognition

Despite following the Google Documentation to utilize the Vision API, I am encountering a persistent CORS error:

Access to XMLHttpRequest at 'https://vision.googleapis.com/v1/images:annotate?key=MY_KEY_HERE' from origin 'https://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.

In a Vue template, here is what I am trying to do:

    const googleRequestData = {
      requests: [
        {
          image: {
            content: base64Image
          },
          features: [
            {
              type: 'LABEL_DETECTION',
              maxResults: 10
            },
            {
              type: 'SAFE_SEARCH_DETECTION'
            }
          ]
        }
      ]
    }
    const gv = await this.$axios.$post(
      `https://vision.googleapis.com/v1/images:annotate?key=${
        process.env.googleApplicationCredentials
      }`,
      googleRequestData
    )
    console.log('google vision', gv)

Although it seems straightforward, I am certain that I am overlooking something due to inadequate documentation. Any assistance would be greatly appreciated. Thank you!

Answer №1

  1. To enable Cross Origin Requests to your Vision Api, consider referencing the documentation on Google Cloud for allowing your domain.

  2. Another option is to set up a server (such as node.js) to serve your Vue.js app and make requests to the Vision API from there. This will prevent CORS blocking and allow you to integrate the api seamlessly with Vue.js. Check out different backend options that can be used with Vue.js here.

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

Is it better to use Rollup for exporting individual components instead of lumping them all into one index.js

Currently, I am working on developing a custom component library using React and Rollup for bundling. The current setup bundles all components into two large files: dist ├ cjs │ └ index.js (1.7mb) └ esm └ index.js (1.7mb) I would like to ...

Accessing JavaScript elements from PHP code

Is it possible to recognize JavaScript elements if they are nested within PHP tags? For example: <?php $username = "maniacal"; echo "<p class='welcome' id='greeting'>Hi, Welcome to the site!!</p>" ?> That's ...

Utilizing Axios Response Data in Vue JS to Enhance User Experience

Recently delved into Vue and not exactly a fan of Javascript, so I've set up a Vue component that uses Axios to fetch some data. However, I'm struggling to display this data in my form elements. I have Axios intercepting the response and retriev ...

Utilizing global context in Next.js version 13 through the implementation of layout.js

I'm currently using Next.js to develop a web application with authentication and global user state. When a user signs in, I want to display their username in the navbar and pass the user state to child components for API calls requiring user informati ...

Retrieving data from the option page's localStorage using the content script

Currently attempting to develop a straightforward chrome extension, however encountering challenges when trying to access the options.html's local storage from my content script "auto.js". After researching and navigating through Chrome's convol ...

- The click function is failing to work after an ajax call [Potential event delegation problem]

I have a webpage where I update the contents of an unordered list using $.get() every 5 seconds. The issue I am facing is that the click function for the list items is not working properly. Even though the list items are being updated correctly, there se ...

In Vue.js, I am looking to pass data from a component to a JS file without involving other components. While I can easily access the data within the component itself, I am facing difficulty in

How can I emit an event from the Abc.vue component and listen to it in a .js file without involving other components? <template> <div class="hello"> <div> <input v-model="clickCount" placeholder="edit me" v-on:change="emit ...

bind a class property dynamically in real-time

I need to dynamically generate a TypeScript class and then add a property to it on the go. The property could be of any type like a function, Promise, etc., and should use this with the intention that it refers to the class itself. let MyClass = class{ ...

Incorporating a specific time to a JavaScript date object

In my Angular application, I am facing an issue with adding a time to a date. The appointmentDate from the date picker returns a JavaScript date while the appointmentTime is selected from a dropdown with options like "8:00", "8:30", "9:00", etc. I'm ...

Puppeteer is experiencing a hanging issue on Raspberry Pi Zero due to a timeout being exceeded

I am encountering difficulties with running puppeteer on my Raspberry Pi Zero, following the steps outlined in this tutorial. Here is what I have attempted so far: $ sudo apt-get install chromium-browser chromium-codecs-ffmpeg --yes $ npm init -Y $ npm ...

Challenges with active class routing in Vuetify's v-btn

Currently, I am developing a CRUD application using Vue in conjunction with Vuetify. In the app, I have defined some links as v-btns and utilized the "to" attribute to specify the routes. Upon observing the behavior of these buttons when clicked, I noticed ...

Sorry, I cannot complete this task as it involves rewriting copyrighted content

I recently implemented the useRef hook in my scroll function, specifying HTMLDivElement as the type. However, I encountered an issue where I received the error message "Property 'clientHeight, scrollHeight, scrollTop' does not exist on type &apos ...

Run JavaScript code whenever the table is modified

I have a dynamic table that loads data asynchronously, and I am looking for a way to trigger a function every time the content of the table changes - whether it's new data being added or modifications to existing data. Is there a method to achieve th ...

Calculate the sum of a parameter in an array when a specific condition is met

I have an array that needs to be processed. var arr = [ {'ResId':123,'ResVal':4,'Date':'11/03/2015'}, {'ResId':123,'ResVal':8,'Date':'11/03/2015'}, {'ResI ...

Every item in my array is replaced by the most recently added element

Take a look at this code snippet on JSFiddle: https://jsfiddle.net/reko91/998woow6/ The issue I am facing with my project is that every time I add an element to an array, it ends up overwriting all the existing elements with the newly added one. To repli ...

Toggle button color on click by using ng-click on a different button

As a newcomer to Angular, I am facing an issue. I have two buttons - button1 and button2. What I want is that when I click on button1, its color changes from green to red. Then, when I click on button2, the color of button1 should revert back to green. How ...

I'm looking to locate the API documentation for AngularJS TypeScript

After transitioning from using AngularJS 1.4 and plain JavaScript to now working with AngularJS 1.5 but utilizing TypeScript, I have found it challenging to find helpful documentation. For instance, when trying to inject services like $q or $timeout into m ...

Setting default parameters for TypeScript generics

Let's say I define a function like this: const myFunc = <T, > (data: T) => { return data?.map((d) => ({name: d.name}) } The TypeScript compiler throws an error saying: Property 'name' does not exist on type 'T', whic ...

Redux: The action was effectively triggered, but the state remained unformed

I'm currently working on a project to familiarize myself with Redux. I am using the Redux DevTools to monitor my two states: lists and todos. However, I am running into an issue where only todos are being displayed, despite trying various troubleshoot ...

What could be causing VueJs2 computed properties to not update?

Check out this updated grid example from the official examples. In my application, I need to reposition the sortOrders array. created: function () { var vm = this; this.columns.forEach(function (key) { vm.sortOrders[key] = 1 }) ...