What is the best way to retrieve search queries from suggestqueries.google.com through a fetch request, or is there another method to obtain Google suggestions?

https://i.sstatic.net/1rTiC.png

I have recently created a Vue.JS new tab page and I'm looking to integrate Google suggestions into the search bar. After some research, I stumbled upon an API that seems like it could help me achieve this. However, when I run the code snippet below:

function retrieveQueries() {
    fetch(
        `http://suggestqueries.google.com/complete/search?client=chrome&q=cats`
    )
        .then((res) => res.json()) //failed when just printing res as the default output, or with res.text()
        .then((data) => {
            console.log(data);
        })
        .catch((err) => {
            console.error(err);
        });
}

Upon execution, I encounter the following error message in the console:

GoogleSearchBar.vue?69f5:42 TypeError: Failed to fetch
eval @ GoogleSearchBar.vue?69f5:42
Promise.catch (async)
retrieveQueries @ GoogleSearchBar.vue?69f5:40
Object.onInput._cache.<computed>._cache.<computed> @ GoogleSearchBar.vue?69f5:14
callWithErrorHandling @ runtime-core.esm-bundler.js?5c40:154
callWithAsyncErrorHandling @ runtime-core.esm-bundler.js?5c40:163
invoker @ runtime-dom.esm-bundler.js?830f:301

However, when testing the same URL using an API Tester, I receive the following response:

["cats",["cats","cats for sale","cats for sale in my area","cats for sale UK"],["","","",""],[],{"google:clientdata":{"bpc":false,"tlw":false},"google:suggestrelevance":[1252,650,601,600],"google:suggestsubtypes":[[433,355],[433,457],[402],[402]],"google:suggesttype":["QUERY","QUERY","QUERY","QUERY"],"google:verbatimrelevance":1300}]

This code functions properly with the openweathermap API, and the response from an API tester indicates that I am getting the desired results. Is there something wrong with my implementation, or is there a more effective approach to achieving this functionality?

Answer №1

In order to retrieve information from a different domain in the user's browser, the Access-Control-Allow-Origin header must be set for your domain. One way to work around this limitation is by using a proxy service like corsanywhere. Here's an example of how the input would appear:

https://corsanywhere.herokuapp.com/http://suggestqueries.google.com/complete/search?client=chrome&q=cats

I modified my original code to include this new link, allowing me to access all the desired data. (Credit goes to aplet123)

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

There is an issue with the functionality of the checkboxes within the loop in Vue.js

When utilizing v-for, I have multiple materials that need to be assigned with a checkbox. However, upon clicking the checkbox, a group of materials is instantly highlighted. This issue arises because the Quantity parameter is the same for numerous material ...

inability to conceal a two-dimensional marker array within Google Maps API v3

I need some help with my marker that refuses to hide Even after using setMap, my marker is still visible on the map Here is the error message from the console Please assist! Thank you in advance markers[i][j].setMap(null); markers.setMap(null); va ...

What is the best way to eliminate a vertical line from the canvas in react-chartjs-2?

Can someone please lend me a hand? I've been working on a project in React JS that involves using react-chartjs-2 to display charts. I'm trying to incorporate a range slider for the chart to manipulate values on the x-axis, as well as two vertic ...

How to make a GET request to a Node server using Angular

I am currently running a node server on port 8000 app.get('/historical/:days' ,(req,res,next){..}) My question is how to send a request from an Angular app (running on port 4200) in the browser to this node server. Below is my attempt: makeReq ...

What is the best way to retrieve widget options when inside an event?

Creating a custom jQuery widget: jQuery.widget("ui.test",{ _init: function(){ $(this.element).click(this.showPoint); }, showPoint: function(E){ E.stopPropagation(); alert(this.options.dir); } } Initializing the cu ...

What are some tips for integrating Vueform Multiselect with Tailwind?

After reviewing the documentation for VueForm Multiselect, I came across a statement that mentions the need to import images into Tailwind and then incorporate the theme into your component. Although this provides some guidance, it falls short on explaini ...

What is the best way to enlarge text size with jquery?

I am attempting to: $('a[data-text="size-bigger"]').click(function () { a=$('span'); b=$('span').css('font-size'); a.css('font-size', b+1); } ); Initially, I ha ...

Showing XML content with JavaScript

Trying to parse a simple XML list using JavaScript, but having trouble formatting it the way I need. <TOURS> <MONTH> <TOUR> <NUMBER>1</NUMBER> <VENUE>City Name - Venue Name</VENUE> < ...

CSS: Unexpected value, received NaNrgb

I was attempting to incorporate a checkbox into a Bootstrap form that turns green when it is checked. Here is the code I used: function updateColor() { $("#check1").animate({ "background-color": "rgb(209, 231, 221)" }); } <script src="http ...

Is it feasible for draggable divs to create a trail of lines as they are dragged?

Looking to create an interactive online coach tactic board where you can easily rearrange player positions and demonstrate specific tactics by dragging divs representing players. Utilizing Touch Punch for smooth dragging functionality on PCs, tablets, and ...

Incorporating session management in Express.js v4 and Socket.io v1

Is there a way to efficiently store and access session data in express.js and use it within socket.io events? I'm currently building a web application with express.js v4, socket.io v1, and utilizing the basic express-session middleware. I've de ...

A dropdown menu generated from a Google Sheet script that allows for the input of custom text alongside predefined

Currently, I am working on creating a dropdown list that not only allows me to select items but also input free text. The code below enables me to choose an item from a selected range in Google Sheets. However, I am looking for a way to incorporate the f ...

Strategies for resolving minor validation challenges (Almost complete, just a few finishing touches needed ...)

This question was originally posted in Spanish on es.stackoverflow.com by Julian Dumitrel: I had planned to make a record, step by step, where I had 3 different steps all within one <form>. After finishing my form validator successfully, I encounte ...

Oops! There was an unexpected error: TypeError - It seems that the property 'title' cannot be read because it is undefined

HTML document example <ion-header> <ion-toolbar color="danger"> <ion-buttons> <button ion-button navPop icon-only> <ion-icon ios="ios-arrow-back" md="md-arrow-back"></ion-icon> </button> ...

Tips for determining the number of columns in a multi-column web layout using webkit

Having some static HTML content that needs to be formatted using multiple columns, which are presented as pages to the user. The method used is quite simple: bodyID = document.getElementsByTagName('body')[0]; bodyID.style.width = desiredWidth; t ...

Submitting a redux form involves using a loop to send multiple values via axios post requests

I have a form with a list of checkboxes and I want to post each value to an axios API on Redux form submit. Below is the code snippet for the submit function: values={example1:true, example2:true, example3:false, example4:true} The expected output in my c ...

What is the best way to activate ui-sref in an AngularJS unit test?

Currently I am conducting a test on an AngularJS view. The code sample contains some non-standard helpers, but they should not affect the specific functionality being tested. Below is the view (written in Jade): #authentication-options button#sign-up(u ...

HTTP Proxy: Discard partially completed requests

My Node.js-based HTTP Proxy has the ability to transform response body on the fly in a streaming fashion. Similar to nginx, my proxy compresses the response body using libz for gzipping. https://i.sstatic.net/VrgX7.jpg However, I am faced with a question: ...

What is the maximum image size that can be displayed on an HTML canvas?

Is there a limit on the size of an image that can be rendered into an HTML5 canvas? I have noticed that images larger than 5mb are not appearing when converted to a DataURL. For example, a 10mb jpg file results in a black screen when opened as a dataURI. ...

Alter the font color upon clicking the menu using jQuery

When I click on the menu and sub-menu items, I want to change their colors along with their parent. You can see an example of how I want it to work here. However, currently, when I click on a sub-menu item, the color of the parent menu item gets removed. ...