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

别来无恙 提交于 2021-02-09 10:58:10

问题


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


回答1:


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.




回答2:


You can enable cross domain request in server with

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


来源:https://stackoverflow.com/questions/37561440/how-to-overcome-cors-redirect-issue-while-performing-post-operation-via-ajax

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!