I have a UI which is built with JS/KnockOut. The UI (Site A) has form
and onclick
functions. I send the data to my Mock API (running on Site B).
But, I get this error due to Cross Origin as both these are on different sites.
I binged/yahooed/googled all the sites and one thing they mentioned was to set the header Access-Control-Allow-Origin
.
- Can you please help with a code snippet on how to do this?
- Should I be adding this header on Site A or Site B?
I tried the following in Site A:
var req = new XMLHttpRequest();
var url = 'http://siteB';
req.open('POST',url,true);
req.setRequestHeader('Access-Control-Allow-Origin','*');
req.setRequestHeader('Access-Control-Allow-Headers','*');
...
$.post('http://siteB/FormUpload', data, function(r)
{
if (r)
{redirectSuccess();}
else
{viewModel.uploadError(true);}
}
Am I missing something?
Thanks!
Update I tried to follow links which @brianlmerritt pointed below and I have the same thing... but still see this issue. Do we need to send the headers before my Post or does it have to be same request?