Python REST API localhost CORS issues - javascript

I'm working on an assignment which needs a python backend and react.js frontend. While both of those parts are working fine individually, I am having some problems dealing with the interaction between the two.
I am using a flask based python REST API for the backend. When the API is hosted on pythonanywhere it works with the frontend perfectly. The problem is that the implementation should be able to be run locally and offline. I tried running the API on localhost:5000 and the front end is running via npm localhost:3000. When I try to access the API this way I get the error: Cross-Origin Request Blocked: The Same Origin Policy disallows reading the remote resource at http://localhost:5000/api/list/all. (Reason: CORS header ‘Access-Control-Allow-Origin’ does not match ‘True’).
I had a similar problem for the hosted version, but using Simple Requests fixed that. The same approach is not working here.
I would appreciate any advice you have.
Thanks in advance.
Edit: Found a solution. I used flasks jsonify method. It turns out the problem was that the data was text and this caused the CORS problem

Related

is it possible to solve Cross-Origin Resource Sharing issue from client side?

i am making a application by using REST API. the api provide me data but browser block for CORS policy. is it possible solve the issue from client side. or need to work at serve. i have try some nodejs package like cors-anywhere but still having issue
It is not possible to solve for good, because it is handled by server to control the origin request (The origin of the request(client) and the server must be in the same origin). But while you are developing you can use cors chrome extension, it will allow you to make the requests.
Chome extension.
Hope it helps.

Issue with fetching data from a web api

So I started watching a tutorial on how to fetch data from a web api, and the first line of code that the instructor writes is this.
fetch('https://www.metaweather.com/api/location/2487956/')
He then explains that you can't fetch data from a website that isn't yours because of the same origin policy and uses a workaround which is using the crossorigin.me site. It works for him in the video, but doesn't work for me and it always gives me this error:
Cross-Origin Request Blocked: The Same Origin Policy disallows reading the remote resource at https://www.metaweather.com/api/location/2487956/. (Reason: CORS header ‘Access-Control-Allow-Origin’ missing)
I've been looking for over an hour and I couldn't find a single solution. I'd really appreciate it if someone could help me out.
Or u can use a cors plugin for chrome and use it
From your question, using fetch() means that you are using either javascript or one of it's frameworks, if I may ask, which method are you using as your request method: POST or GET?

CORS error when calling from the client?

I have a Nginx webserver in front of a Node REST API delivering JSON formatted data.
I also have a web app which consumes the above API and works fine for a majority of the requests it makes but sometimes, for certain URLs, the client gets a CORs error aka an 'Access-Control-Allow-Origin' error.
When I call the data again from the server of the web app it works fine again.
Could anyone shed some light on this issue.
I am using axios to call the API from the web app
You need to add CORS headers in response so that api can be accessed from browser.
You can use npm cors package https://www.npmjs.com/package/cors

How to due with the cross domain in webpack

My environment is Nodejs, and I want to make request(http) to api in the internet.
But there are some problems, after using webpack, the console would show me cross
domain error. I guess it is because my api and webpack-server have diffrent in
port. What should I do to solve the problem.
The issue you are experiencing is caused by the server not properly sending a CORS header to allow the request.
If the API is one of your own, you can enable CORS on your server for one, many, or all domains that attempt to access the API. How to do this depends on your server configuration.
If the API is provided by someone else, you may need to contact them or consult their documentation on how to perform a CORS request to their services.

Why google place api integration not worked

json data while follow the given link but it is worked when I integrate in our project.
https://maps.googleapis.com/maps/api/place/nearbysearch/json?location=-33.8670,151.1957&radius=500&types=food&name=cruise&key=AIzaSyCaLjRTWe0j9RKu9qG4_9VovLltLtU_hQ8
Servers can enable CORS, meaning that they will accept requests from other domains. However, each server is different and most APIs still do not allow cross-origin requests. It looks like api.geonames.org is one of these that does not support CORS.
Note that CORS only applies to requests sent from a browser. Hitting that API from a backend server (like Node or Rails) will work just fine.

Categories

Resources