Sunday, May 19, 2024
 Popular · Latest · Hot · Upcoming
31
rated 0 times [  36] [ 5]  / answers: 1 / hits: 20073  / 5 Years ago, fri, august 9, 2019, 12:00:00

I'm trying to fetch data from an API using reactJS, I used postman to test the API and it returns a JSON, but when I try from my reactJS application the web browser returns this error.



Access to fetch at 'http://XX.XX.XX.XX*/v1/shipping' from origin 'http://localhost:3000' 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. If an opaque response serves your needs, set the request's mode to 'no-cors' to fetch the resource with CORS disabled.



Does anyone know why it's not working in the browser, but it works on postman?



web browser error



postman response



Here is the fetch function.



   fetchingData = () => {
const bodyReq = {
bu:,
customer:,
start_time:,
end_time:,
serial_number:XXXXXXXXXXXXX,XXXXXXXXXXXXX
}
fetch(http://XX.XX.XX.XX/v1/shipping, {
method: 'POST',
mode: 'cors',
cache: 'no-cache',
credentials: 'same-origin',
headers: {
'Accept': 'application/json',
'Content-Type': 'application/json',
'Access-Control-Allow-Origin': '*'
},
body: JSON.stringify({bodyReq}),
})
.then(response => {response.json()
console.log(response, 'Response')})
.then(
(response) => {
this.setState({
isLoaded: true,
items: response.items
});
console.log(response, 'Response')
},
(error) => {
this.setState({
isLoaded: true,
error
});
console.log(error, 'RESerror')
}
)
}


This is the response I'm expecting from the API



{
code: 201,
error_code: 0,
msg: [
{
bu: XXXX,
carton_no: XXXXXX,
container_no: XXXXXX,
container_type: null,
cust_no: XXXXXX,
emp_no: XXXXXX,
group_name: XXXXXX,
in_station_time: XXXX-XX-XX XX:XX:XX,
line_name: XXXXXX,
mo_number: XXXXXX,
model_name: XXXXXX,
pallet_no: XXXXXX,
plate_no: null,
po_number: XXXXXX,
qa_no: N/A,
serial_number: XXXXXX,
ship_address: XXXXXX,
ship_mothod: XXXXXX,
version_code: XXXXXX
},
{
bu: XXXX,
carton_no: XXXXXX,
container_no: XXXXXX,
container_type: null,
cust_no: XXXXXX,
emp_no: XXXXXX,
group_name: XXXXXX,
in_station_time: XXXX-XX-XX XX:XX:XX,
line_name: XXXXXX,
mo_number: XXXXXX,
model_name: XXXXXX,
pallet_no: XXXXXX,
plate_no: null,
po_number: XXXXXX,
qa_no: N/A,
serial_number: XXXXXX,
ship_address: XXXXXX,
ship_mothod: XXXXXX,
version_code: XXXXXX
}
],
request: POST /v1/shipping
}


*note: I had to censure some data because is confidential data from the enterprise.


More From » reactjs

 Answers
2

The browser environment is different from Postman. A browser has security features that Postman doesn't have and CORS is one of those.



If you want to understand how CORS work, check this link: https://developer.mozilla.org/en-US/docs/Web/HTTP/CORS




Cross-Origin Resource Sharing (CORS) is a mechanism that uses additional HTTP headers to tell a browser to let a web application running at one origin (domain) have permission to access selected resources from a server at a different origin. A web application executes a cross-origin HTTP request when it requests a resource that has a different origin (domain, protocol, or port) than its own origin.



[#51785] Thursday, August 1, 2019, 5 Years  [reply] [flag answer]
Only authorized users can answer the question. Please sign in first, or register a free account.
jericho

Total Points: 204
Total Questions: 98
Total Answers: 108

Location: Thailand
Member since Thu, Apr 22, 2021
3 Years ago
;