-1

I have been stuck for a while with the CORS issue. I have been checking numerous documentation and I still can't fix this issue and I am not sure what I am missing. I think this is a classic fetch:

fetch('http://192.168.1.1/osc/commands/execute', {
  method:'POST',
  headers:{
    'Content-Type': 'application/json',
    'Access-Control-Allow-Origin':'*' ,
    'X-Requested-With':'xmlhttprequest'
  },
  mode:'cors',
  body:JSON.stringify({
    name: 'camera.startSession',
    parameters: {}
  })
 })
 .then(response => response.json())
 .then(console.log)
}

but I still have this error: Cross-Origin Request Blocked: The Same Origin Policy disallows reading the remote resource at http://192.168.1.1/osc/commands/execute. (Reason: CORS header ‘Access-Control-Allow-Origin’ missing). This is followed by : Cross-Origin Request Blocked: The Same Origin Policy disallows reading the remote resource at http://192.168.1.1/osc/commands/execute. (Reason: CORS request did not succeed) Is that supposed the headers 'Access-Control-Allow-Origin':''* was supposed to fix this issue? I have been successfully testing this request with Talend API Tester on chrome, but the option copy as fetch still response with the same errors...

Thank you for your assistance. Cheers.

Raskar Kapak
  • 107
  • 1
  • 2
  • is it allowed from the server side ? – brk Nov 09 '20 at 04:46
  • I cannot access to this server but it should be as it is working when I am using the Talend API tester on Chrome. – Raskar Kapak Nov 09 '20 at 05:08
  • 1
    CORS is not allowed on the server. When the server does not allow CORS you can still call its api from your localhost when there is no origin. For example if you use a html file and open it with your browser to call the methods it will work but if you upload the same file on a web server then open it and call it you will get same-origin error. So probably CORS is not allowed. – nAviD Nov 09 '20 at 05:31

1 Answers1

0

'Access-Control-Allow-Origin':'*' in response header not in request header would solve the problem. As it needs to be in response header, server side modification needed.

MD Ruhul Amin
  • 4,386
  • 1
  • 22
  • 37