CORS policy prevents REST service POST from Snap app preview in Chrome browser

CORS policy prevents REST service POST from Snap app preview in Chrome browser

Hello SOTI team,

I am developing an app with SOTI-SNAP wherein I use javascript to use a AspNetCore REST service from our server located on the internet.
When developing I use the preview function of SOTI-Snap in the browser (Chrome). Calls with the GET method works from preview. But the POST method returns the following error in the browser console:

Access to XMLHttpRequest at 'https://[OurServer].com/soticlean/TimeRecords' from origin 'https://s3.eu-central-1.amazonaws.com' has been blocked by CORS policy: Response to preflight request doesn't pass access control check: It does not have HTTP ok status.

 

Here my Javascript call from my app:

var settings = {
"url": Snap.variables.getValue("gvarServiceURL"),
"method": "POST",
"timeout": 0,
"headers": {
"Content-Type": "application/json"
},
"data": JSON.stringify(
{
"deviceUid": data.deviceUid,
"date": data.date
}),
};

return $.ajax(settings).done(function (response) {
console.log(JSON.stringify(response));
alert(JSON.stringify(response));

}).fail(function (response) {
alert(JSON.stringify(response));
});
}

 

And here the customHeader section from my Web.config of my AspNetCore REST Service:

...

<system.webServer>
<httpProtocol>
<customHeaders>
<add name="Access-Control-Allow-Origin" value="https://s3.eu-central-1.amazonaws.com" />
<add name="Access-Control-Allow-Headers" value="Content-Type"/>
<add name="Access-Control-Allow-Methods" value="GET,PUT,POST,DELETE,OPTIONS" />
</customHeaders>
</httpProtocol>

....

 

I hope someone can help me.

Thanks a lot

Martin

  • 14 October 2021
  • SOTI Snap
  • 3 Answers
  • 0 Upvote
  • 3 Followers
  • 242 Views
    • 3 Answers
    • 0 Upvote
    • 3 Followers

3 Answers

Order By:   Standard | Newest | Votes
Deepak Kumar | posted this 18 October 2021

Hi Martin,

Are you able to call these APIs using any other tool (e.g Postman etc.)?

  • 0
  • 0
Gundolf Archner | posted this 19 October 2021

Hi Deepak,

thank you very much for your fast answer.

We try it and will give you our feedback.

 

Kind regards

Gundolf

  • 0
  • 0
Martin H. | posted this 26 October 2021

Hi Deepak,

we were able to solve our problem.
In our REST service, the implementation of the "Options" method for handling the preflight requests from clients was missing.

Now it works.

Thanks

 

  • 1
  • 0

Give us your feedback
Give us your feedback
Feedback