exploring CORS settings in fetch API for secure GET requests

My endpoint is localhost:8080/enquiry

and it returns the following JSON:

[{"_id":"5a283e4c5a36f4556af34742",
   "firstName":"bob",
   "surname":"hoskins",
   "telephoneNumber":939483948,
   "gender":"male",
   "dayOfBirth":17,
   "monthOfBirth":5,
   "yearOfBirth":1978,"comments":"hello",
   "emailAddress":"<a href="/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="046e6d696977767677446369656d682a6769">[email protected]</a>",
   "createdAt":"2017-12-06T19:00:28.401Z"}]

The action creator I am using is:

export const receiveEnquiries = enquiries => ({
    type: RECEIVE_ENQUIRIES,
    enquiries 

  });
export const fetchEnquiries = () => dispatch => (
    enquiryAPIUtil
        .fetchEnquiries() // this is the fetch call in api.js below
        .then((enquiries) => {dispatch(receiveEnquiries(enquiries))})
    );

The fetch request in my api.js file looks like this:

export const fetchEnquiries = () =>
    fetch(`${api}/enquiry`, { headers })
    .then(res => {
        res.json()
        console.log("res",res)
    })
    .then(data => console.log("data",data))

Instead of logging the expected JSON, it logs:

res Response {type: "cors", url: "http://localhost:8080/enquiry", 
redirected: false, status: 200, ok: true, …} 

In my express server.js file, I have included:

 const cors = require('cors')
 app.use(cors())

as well as code to render the JSON data.

I'm trying to figure out if the issue lies on the client side or the server side?

Answer №1

res.json() will give you a promise that contains the parsed JSON object.

If you disregard the returned object, you won't receive any JSON data.

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

What is the best way to present retrieved JSON data from a database in Node.js without using the JSON format?

Here is the code snippet: var http=require("http"); var fs = require("fs"); var express = require("express"); var app = express(); var path = require("path"); var mysql = require('mysql'); var ejs = require("ejs") var bodyParser = require(&apos ...

Why is it necessary for me to prefix every single one of my route handlers?

Currently, I am developing an application utilizing Expressjs framework running on node within IISNode (specifically on windows). To begin the setup process, I followed examples provided by various expressjs resources: backend.configure(function() { ...

Navbar in bootstrap appears to be flashing when it is in its expanded

Example Link On smaller screens, the bootstrap navbar menu does not collapse by default when clicking on a menu item. To fix this issue, I added attributes data-toggle="collapse" and data-target="#navbar-collapse" to each menu item so that the menu will ...

The art of replacing material-ui styles with styled components

As a newcomer to UI material design, I am eager to create my own customized Button Component using styled-components. I am facing a challenge in overriding the CSS based on different button variations such as "primary" or "secondary". You can find my cod ...

Instantly magnifying on the initial point regardless of the zoom type chosen is a feature of Chart.js zoom

Despite adding zoom functionality to my line chart, I am facing an issue where it automatically zooms in to the first point and does not allow me to zoom back out, except for using the reset zoom function. The zoom out function is also not working properly ...

I require a way to decelerate the speed of the roulette wheel's rotation

For my assignment, I'm tasked with creating a roulette spin wheel code in JavaScript without using any plugins. I need to incorporate some conditions before executing the code, particularly for slowing down the speed of the roulette spin. Additionally ...

Ensuring that touch events are activated on Firefox's desktop browser

Testing on a Windows 7 desktop with touch capabilities, I performed a simple test that looked something like this: temp_div.addEventListener('touchstart', function(e){ /*confirm */ }, false) temp_div.addEventListener('pointerdown', fun ...

Stop the React timer count for a moment

I am currently attempting to implement a pause feature in a countdown timer using React. Here is the code snippet: export default function Home() { const timerRef = useRef(180) const [minute, setMinute] = useState(3) const [second, setSecond] = useS ...

Tips for utilizing an observable in Angular without subscribing to it

I am currently working on an Angular 8 application that involves making API calls. Here is an example: getDossierEntry(patientUUID: string, type: String = ''): Observable<DossierEntry[]> { const entryType = type === '' ? ' ...

Utilize typehead.js in Python Django to retrieve an updated data list directly from the database

file.js var source = new Bloodhound({ hint: false, datumTokenizer: Bloodhound.tokenizers.obj.whitespace("description"), queryTokenizer: Bloodhound.tokenizers.whitespace, // /a_c/p_s/?term=d&category=all remote: "/a ...

Best return type for a webservice triggered using jQuery but not requiring any data to be returned

I am currently working on a web service that utilizes several methods. These methods do not have significant impact on the client side as I call them using jQuery/ajax. My main concern is regarding the recommended return type. Typically, I would return JS ...

A clever JavaScript function generator encapsulated within an instant function nested within a jQuery ready statement

This code snippet features an immediate function enclosed within a jQuery ready function. $((function(_this) { return function() { document.write('called!'); }; })(this)); I am puzzled by where the resultant function ...

Convert the dynamic table and save it as a JSON file

Looking for the most effective method to save dynamic table data into JSON format. I have two tables that need to be saved into a single JSON file. While I can easily access and console log the regular table data, I'm having trouble retrieving the td ...

Provide JSON data from the index method to the index.html file located in the public directory

I'm currently working on building a single page application using Rails as the backend, since it's the only framework I am familiar with. All my project files are stored in the public folder, and I've also set up npm and jspm to incorporate ...

The onclick function only triggers after the second click

I have an issue with my image view onclick function code. Currently, the code works perfectly but only when I click for the second time. I am not sure what the problem is and would appreciate any help in fixing it. Below is the code snippet: <script ...

When I select an option with an object, ng-model is receiving '[object Object]' instead of the actual object for <select> element

Referencing an example from Angular documentation: this link, specifically the section on "Using ngValue to bind the model to an array of objects." In the index.html file: <!doctype html> <html lang="en"> <head> <meta charset="UTF- ...

dynamic dropdown displaying colors for selection

Using a bootstrap form to add a printer involves selecting the material and color. The dynamic dropdowns are functioning correctly, but it is necessary to display the selected color for user clarity. Please guide me on how to pass the color along with its ...

Converting city/country combinations to timezones using Node.js: A comprehensive guide

When provided with the name of a city and country, what is the most reliable method for determining its timezone? ...

Update a specific ListItem in a List when the key is already present (ReactJS with MaterialUI)

Just a heads up, I'm working with material-ui using ReactJS and following the Redux pattern. Issue: I have an array that is being displayed in a List component with individual ListItems. However, when the array gets updated, the props change trigger ...

Using Javascript to establish a connection with a Joomla MySQL database

Recently, I was tasked with building a website in Joomla that utilizes a MySQL database. As part of this project, I am required to develop a JavaScript function that connects to the MySQL database. Do you have any advice or tips for me? ...