MD Danish
MD Danish

Reputation: 189

How to overcome CORS redirect issue while performing post operation via ajax?

I'm able to get signed in to the roundcube instance (hosted at another server) via form submission with post method type from external login form.

I'm getting this error (While signing via ajax):

XMLHttpRequest cannot load https://192.168.0.7/mail/. The request was redirected to 'https://192.168.0.7/mail/?_task=mail&_token=36e97d50951472c4c65de562a0109e94', which is disallowed for cross-origin requests that require preflight.

$.ajax({
    type: 'POST',
    url: 'https://192.168.0.7/mail/',
    data: {
        _user: 'myusername', _pass:'mypassword', _autologin: 1, _action: 'login',_task: 'login'
    },
    success: function( data ){
        $('#result').html(data);
    },
    crossDomain: true,
});

I have overcome all CORS related issue except this one. I know it is default browsers behavior to disallow redirection at cross-sites. Please Suggest, If there is a way to get this problem solved or should i move on to look for another way?

Thanks

Upvotes: 4

Views: 3106

Answers (2)

Pritam Banerjee
Pritam Banerjee

Reputation: 18923

You should try this:

header('Access-Control-Allow-Origin: *');
header('Access-Control-Allow-Methods: GET, POST, OPTIONS'); 

You should add all methods in the header that you need access to.

Upvotes: 1

mujuonly
mujuonly

Reputation: 11861

You can enable cross domain request in server with

header('Access-Control-Allow-Origin: *');
header('Access-Control-Allow-Methods: GET, POST'); 

Upvotes: 1

Related Questions