When I try to GET request from my front end project I am getting this error
Access to XMLHttpRequest at ‘http://10.3.0.112:41052/operations’ from origin ‘http://10.3.0.112:5488’ 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.
An above code is my nginx config file.
events {}
http {
upstream tag {
server tag:41038;
server tag:42038;
}
server {
listen 41038;
listen 42038;
location / {
proxy_pass http://tag;
}
}
upstream order {
server order:41052;
server order:42052;
server order:43052;
}
server {
listen 41052;
listen 42052;
listen 43052;
location / {
proxy_pass http://order;
}
}
}
And the last one is from my front-end application ajax request
function allCompletedJobOrders() {
requestStarted()
if(hasSelectableProps.length > 0) {
let stationIds = hasSelectableProps.map(item => item.id)
completedJobOrders = []
jobOrders = []
$.ajax({
type: "GET",
url: `http://${getConfig('serverAddress')}:41052/job-orders?stationIds=${stationIds}`,
headers: {
'Accept': 'application/json',
'Content-Type': 'application/json',
'Authorization': auth ? `${auth.token_type} ${auth.access_token}` : null
},
success: function (data) {
if(data.length > 0){
data.filter(item => item.startTime && item.endTime).forEach(jobOrder => {
completedJobOrders.push({
id: jobOrder.id,
code:jobOrder.code,
stationId:jobOrder.stationId,
startTime:jobOrder.startTime,
endTime: jobOrder.endTime,
targetAmount:jobOrder.targetAmount,
})
})
data.filter(item => !item.endTime).forEach(jobOrder => {
jobOrders.push({
id: jobOrder.id,
code:jobOrder.code,
stationId:jobOrder.stationId,
startTime:jobOrder.startTime,
endTime: jobOrder.endTime,
targetAmount:jobOrder.targetAmount,
operationId: jobOrder.operations[0] ? jobOrder.operations[0].operationId : [],
})
})
requestEnded()
let jobOrderIds = jobOrders.map(item => item.id)
getRollersByIds(jobOrderIds)
}
}
})
}
}
I tried change my nginx config file, but it didn’t work. I need to complete all request from my front end application
2
Answers
CORS stands for Cross Origin Resource Sharing.
CORS issue in not related to nginx rather it is a configuration that we need to apply in our backend. We get this error when our browser URL and API URL are different from each other thus the term Cross Origin.
Simply include your origin i.e.
'http://10.3.0.112:5488'
on the cors allowed origin in the backend and it should work.The following is an example in Node.js.
Try to configure your config file like
Or use chrome extension Allow CORS: Access-Control-Allow-Origin