通过ajax执行后期操作时,如何克服CORS重定向问题? [英] How to overcome CORS redirect issue while performing post operation via ajax?
问题描述
我可以通过表单提交(外部登录表单中的post方法类型)登录到roundcube实例(托管在另一台服务器上).
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.
我遇到此错误(通过ajax签名时):
I'm getting this error (While signing via ajax):
XMLHttpRequest无法加载 https://192.168.0.7/mail/.该请求已重定向到' https://192.168.0.7/mail/?_task=mail&; _token = 36e97d50951472c4c65de562a0109e94 ",对于需要预检的跨域请求是不允许的.
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,
});
除此问题外,我已经解决了所有与CORS相关的问题.我知道禁止跨站点重定向是默认的浏览器行为.请提出建议,如果有解决此问题的方法,还是我应该继续寻找其他方法?
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?
谢谢
推荐答案
您应该尝试以下操作:
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.
这篇关于通过ajax执行后期操作时,如何克服CORS重定向问题?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!