No Access-Control-Allow-Origin header is present on the requested resource angular 9

Riderman Sousa

unread,

Jan 22, 2016, 2:46:45 AM1/22/16

to mountebank-discuss

Error when I try to POST

XMLHttpRequest cannot load XX. Response to preflight request doesn't pass access control check: A wildcard '*' cannot be used in the 'Access-Control-Allow-Origin' header when the credentials flag is true. Origin '' is therefore not allowed access.


I start mountbank

mb --configfile ./test/imposters.ejs --allowInjection --allowCORS


Configure the imposters

imposters.ejs

{
   
"imposters": [ <% include irisServer.ejs %> ]
}

{
   
"port": 8124,
   
"protocol": "http",
   
"stubs": [
       
{
           
"predicates": [{
               
"equals": { "method": "POST", "path": "/login" }
             
}],
           
"responses": [{ "inject": "<%- stringify(filename, 'authResponse.js') %>" }]
       
}
   
]
}

authResponse.js

function stateResponse(request, state, logger) {
   
var parsedBody = JSON.parse(request.body);
   
return {
        headers
: {
           
'Content-Type': 'application/json',
           
'Access-Control-Allow-Origin' : '*'
       
},
        body
: JSON.stringify({ 'token': MD5(parsedBody.user + 'secret'), 'user': parsedBody.user }); // OMITED MD5 FUNCTION IS TO BIG
   
};
}

I configure the $http to.. 

$http.defaults.useXDomain = true;
$http
.defaults.withCredentials = true;
delete $http.defaults.headers.common['X-Requested-With'];
$http
.defaults.headers.common['Access-Control-Allow-Origin'] = '*';

Brandon Byars

unread,

Jan 22, 2016, 4:57:18 AM1/22/16

to Riderman Sousa, mountebank-discuss

Hi there,

Can you try adding the Access-Control-Allow-Credentials: true header to the imposter and letting me know if that fixes it?

-Brandon

Riderman Sousa

unread,

Jan 22, 2016, 7:57:04 PM1/22/16

to mountebank-discuss,

Hi Brandon.. 

Same error..

XMLHttpRequest cannot load http://localhost:8124/login. Response to preflight request doesn't pass access control check: No 'Access-Control-Allow-Origin' header is present on the requested resource. Origin 'http://localhost:8080' is therefore not allowed access.

I change the authResponse,js to

function stateResponse(request, state, logger) {
   
var parsedBody = JSON.parse(request.body);
   
return {
        headers
: {
           
'Content-Type': 'application/json',
           
'Access-Control-Allow-Origin' : '*',


             
'Access-Control-Allow-Credentials': true

Brandon Byars

unread,

Jan 26, 2016, 12:02:37 AM1/26/16

to Riderman Sousa, mountebank-discuss

Hi,

It looks like the solution is to explicitly set the domain (e.g. localhost:8124), or to set withCredentials=false.

-Brandon

Riderman Sousa

unread,

Jan 26, 2016, 12:34:32 AM1/26/16

to mountebank-discuss,

I saw this article :) and changed the answer to ..

function stateResponse(request, state, logger) {
   
var parsedBody = JSON.parse(request.body);return {

        headers

: {
           
'Content-Type': 'application/json',


           
'Access-Control-Allow-Origin' : 'localhost:8080',
           
'Access-Control-Allow-Credentials': 'true',
           
'Access-Control-Allow-Methods': 'POST, GET, OPTIONS',


       
},

        body

: JSON.stringify({ 'token': MD5(parsedBody.user + 'secret'), 'user': parsedBody.user })
   
};
}

I try add too.. 

'Access-Control-Allow-Headers': 'accept, accept-encoding, access-control-allow-headers, access-control-allow-origin, content-type, accept-language'

But same error.

--
You received this message because you are subscribed to the Google Groups "mountebank-discuss" group.

Riderman Sousa

unread,

Jan 26, 2016, 12:36:02 AM1/26/16

to mountebank-discuss,

headers: {
           
'Content-Type': 'application/json',
           
'Access-Control-Allow-Origin' : 'localhost:8080',
           
'Access-Control-Allow-Credentials': 'true',
           
'Access-Control-Allow-Methods': 'POST, GET, OPTIONS',


           
'Access-Control-Allow-Headers': 'accept, accept-encoding, access-control-allow-headers, access-control-allow-origin, content-type, accept-language'

Riderman Sousa

unread,

Jan 26, 2016, 4:06:49 AM1/26/16

to mountebank-discuss

Hey @Brandon Byars.. I made a demo app..

Just clone.. install.. start mb with `npm run test:serve` and start app ´npm run start´

Try to login and you see the error in console.

Brandon Byars

unread,

Jan 28, 2016, 11:21:37 PM1/28/16

to Riderman Sousa, mountebank-discuss

Thanks for the help.  I should have time tonight to work on this.

-Brandon

Riderman Sousa

unread,

Jan 29, 2016, 1:07:45 AM1/29/16

to mountebank-discuss,

Ok..

Thanks for help me @Brandon.

--
You received this message because you are subscribed to the Google Groups "mountebank-discuss" group.

--
You received this message because you are subscribed to the Google Groups "mountebank-discuss" group.

Message has been deleted

unread,

Jun 7, 2017, 10:12:12 PM6/7/17

to mountebank-discuss,

unread,

Jul 9, 2019, 7:00:59 PM7/9/19

to mountebank-discuss

How do you fix no Access

How do I fix the No access-control error on Windows 10?.
Make changes at the server level. If you have access to the server, enable CROS requests by adding Access-Control-Allow-Origin: * header. ... .
Run your own proxy server. ... .
Disable the Same Origin Policy in your browser..

How do I fix CORS policy no Access

< access-control-allow-origin: * You can solve this temporarily by using the Firefox add-on, CORS Everywhere. Just open Firefox, press Ctrl+Shift+A , search the add-on and add it! Thanks this helps to avoid all the hassle and test the code from localhost.

What is CORS error in angular?

So in the previous case of a decoupled application, if your front end tries to request a resource from your server, the browser would throw an error. Since each is of a different origin, they aren't allowed to share resources with each other. We call this the CORS error. CORS error due to browser's same origin policy.

How do I fix blocked by CORS policy?

Use a Chrome extension to add Access-Control-Allow-Origin header into every response. To find one of them, just head over to Chrome Webstore and type in "CORS", dozens will show up in the search result. Or you can install CORS Helper, CORS Unblock or dyna CORS right away.