I am trying to make an API call through Axios in my React Application.However, Iam getting this CORS issue on my browser. I am wondering if i can resolve this issue from a client side as i donot have any access to the API internally.Attached is my code.
const response = axios({
method: 'post',
dataType: 'jsonp',
url: 'https://awww.api.com',
data: {
'appToken':'',
'request':{
'applicationName':'ddfdf',
'userName':'jaime@dfd.com',
'password':'dfd',
'seasonIds':[1521ddfdfd5da02]
}
}
});
return{
type:SHARE_REVIEW,
payload:'response'
}
}
Attached is my WebPack.config.js
module.exports = {
entry: [
'./src/index.js'
],
output: {
path: __dirname,
publicPath: '/',
filename: 'bundle.js'
},
module: {
loaders: [{
exclude: /node_modules/,
loader: 'babel',
query: {
presets: ['react', 'es2015', 'stage-1']
}
},
{ test: /\.json$/, loader: "json-loader"}]
},
resolve: {
extensions: ['', '.js', '.jsx']
},
devServer: {
historyApiFallback: true,
contentBase: './'
},
node: {
dns: 'mock',
net: 'mock'
},
};
Temporary solve this issue by a chrome plugin called CORS. Btw backend server have to send proper header to front end requests.
You can set up a express proxy server using http-proxy-middleware to bypass CORS:
From your react app all requests should be sent to /proxy endpoint and they will be redirected to the intended server.
the simplest way what I found from a tutorial of "TraversyMedia" is that just use https://cors-anywhere.herokuapp.com in 'axios' or 'fetch' api
e.g.
and in your case edit url as
Another way besides @Nahush's answer, if you are already using Express framework in the project then you can avoid using Nginx for reverse-proxy.
A simpler way is to use express-http-proxy
run
npm run build
to create the bundle.Use "/api/api-server" from react code to call the API.
The ideal way would be to add CORS support to your server.
You could also try using a separate jsonp module. As far as I know axios does not support jsonp. So I am not sure if the method you are using would qualify as a valid jsonp request.
There is another hackish work around for the CORS problem. You will have to deploy your code with an nginx server serving as a proxy for both your server and your client. The thing that will do the trick us the
proxy_pass
directive. Configure your nginx server in such a way that the location block handling your particular request willproxy_pass
or redirect your request to your actual server. CORS problems usually occur because of change in the website domain. When you have a singly proxy serving as the face of you client and you server, the browser is fooled into thinking that the server and client reside in the same domain. Ergo no CORS.Consider this example.
Your server is
my-server.com
and your client ismy-client.com
Configure nginx as follows:Here
my-website.com
will be the resultant name of the website where the code will be accessible (name of the proxy website). Once nginx is configured this way. You will need to modify the requests such that:my-server.com/<API-path>
tomy-website.com/server/<API-path>
In case you are not familiar with nginx I would advise you to go through the documentation.
To explain what is happening in the configuration above in brief:
upstream
s define the actual servers to whom the requests will be redirectedserver
block is used to define the actual behaviour of the nginx server.server_name
is used to identify the block which will be used to handle the current request.error_log
andaccess_log
directives are used to define the locations of the log files (used for debugging)location
blocks define the handling of different types of requests:/
all these requests are redirected to the client/server/<API-path>
. We will be redirecting all such requests to the server.Note:
/server
here is being used to distinguish the client side requests from the server side requests. Since the domain is the same there is no other way of distinguishing requests. Keep in mind there is no such convention that compels you to add/server
in all such use cases. It can be changed to any other string eg./my-server/<API-path>
,/abc/<API-path>
, etc.Even though this technique should do the trick, I would highly advise you to add CORS support to the server as this is the ideal way situations like these should be handled.
If you wish to avoid doing all this while developing you could for this chrome extension. It should allow you to perform cross domain requests during development.