CORS problem in angular app

9,709 views
Skip to first unread message

Riderman Sousa

unread,
Jan 21, 2016, 12:46:45 PM1/21/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 %> ]
}

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 21, 2016, 2:57:18 PM1/21/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

--
You received this message because you are subscribed to the Google Groups "mountebank-discuss" group.
To unsubscribe from this group and stop receiving emails from it, send an email to mountebank-disc...@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Riderman Sousa

unread,
Jan 22, 2016, 5:57:04 AM1/22/16
to mountebank-discuss, rider...@gmail.com
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
To unsubscribe from this group and stop receiving emails from it, send an email to mountebank-discuss+unsub...@googlegroups.com.

Brandon Byars

unread,
Jan 25, 2016, 10:02:37 AM1/25/16
to Riderman Sousa, mountebank-discuss
Hi,
So apparently CORS disallows using a wildcard Access-Control-Allow-Origin when credentials is set to true.  See this Stack Overflow for reference: http://stackoverflow.com/questions/19743396/cors-cannot-use-wildcard-in-access-control-allow-origin-when-credentials-flag-i

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

To unsubscribe from this group and stop receiving emails from it, send an email to mountebank-disc...@googlegroups.com.

For more options, visit https://groups.google.com/d/optout.

--
You received this message because you are subscribed to the Google Groups "mountebank-discuss" group.
To unsubscribe from this group and stop receiving emails from it, send an email to mountebank-disc...@googlegroups.com.

Riderman Sousa

unread,
Jan 25, 2016, 10:34:32 AM1/25/16
to mountebank-discuss, rider...@gmail.com
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.
To unsubscribe from this group and stop receiving emails from it, send an email to mountebank-discuss+unsub...@googlegroups.com.

For more options, visit https://groups.google.com/d/optout.

--
You received this message because you are subscribed to the Google Groups "mountebank-discuss" group.
To unsubscribe from this group and stop receiving emails from it, send an email to mountebank-discuss+unsub...@googlegroups.com.

Riderman Sousa

unread,
Jan 25, 2016, 10:36:02 AM1/25/16
to mountebank-discuss, rider...@gmail.com
I try with ..

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'

       
},

too, but same error.

Riderman Sousa

unread,
Jan 25, 2016, 2:06:49 PM1/25/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, 9:21:37 AM1/28/16
to Riderman Sousa, mountebank-discuss
Thanks for the help.  I should have time tonight to work on this.
-Brandon

To unsubscribe from this group and stop receiving emails from it, send an email to mountebank-disc...@googlegroups.com.

For more options, visit https://groups.google.com/d/optout.

--
You received this message because you are subscribed to the Google Groups "mountebank-discuss" group.
To unsubscribe from this group and stop receiving emails from it, send an email to mountebank-disc...@googlegroups.com.

For more options, visit https://groups.google.com/d/optout.

--
You received this message because you are subscribed to the Google Groups "mountebank-discuss" group.
To unsubscribe from this group and stop receiving emails from it, send an email to mountebank-disc...@googlegroups.com.

Riderman Sousa

unread,
Jan 28, 2016, 11:07:45 AM1/28/16
to mountebank-discuss, rider...@gmail.com
Ok..

Thanks for help me @Brandon.

To unsubscribe from this group and stop receiving emails from it, send an email to mountebank-discuss+unsub...@googlegroups.com.

For more options, visit https://groups.google.com/d/optout.

--
You received this message because you are subscribed to the Google Groups "mountebank-discuss" group.
To unsubscribe from this group and stop receiving emails from it, send an email to mountebank-discuss+unsub...@googlegroups.com.

For more options, visit https://groups.google.com/d/optout.

--
You received this message because you are subscribed to the Google Groups "mountebank-discuss" group.
To unsubscribe from this group and stop receiving emails from it, send an email to mountebank-discuss+unsub...@googlegroups.com.
Message has been deleted

atsconsei...@gmail.com

unread,
Jun 7, 2017, 9:12:12 AM6/7/17
to mountebank-discuss, rider...@gmail.com
> To unsubscribe from this group and stop receiving emails from it, send an email to mountebank-disc...@googlegroups.com.
>
> For more options, visit https://groups.google.com/d/optout.
>
>
>
>
>
>
>
>
>
>
> --
>
> You received this message because you are subscribed to the Google Groups "mountebank-discuss" group.
>
> To unsubscribe from this group and stop receiving emails from it, send an email to mountebank-disc...@googlegroups.com.
>
> For more options, visit https://groups.google.com/d/optout.
>
>
>
>
>
>
>
>
>
>
> --
>
> You received this message because you are subscribed to the Google Groups "mountebank-discuss" group.
>
> To unsubscribe from this group and stop receiving emails from it, send an email to mountebank-disc...@googlegroups.com.
>
> For more options, visit https://groups.google.com/d/optout.

Hi,

Any solution for ur problem?
i have the same.
Thanks for all

d.nit...@gmail.com

unread,
Jul 9, 2019, 6:00:59 AM7/9/19
to mountebank-discuss

Use proxy file in your angular app
Follow this video-

https://www.youtube.com/watch?v=z1MUmTjYKH8

Guru Prasad

unread,
Apr 4, 2023, 1:59:49 AM4/4/23
to mountebank-discuss
Hi Team,

I would like to learn Mountebank.
Can anyone have Mountebank videos pls send to me.
I'd be grateful if you could provide videos ASAP.


Thanks & Regards,
Guruprasad.
Reply all
Reply to author
Forward
0 new messages