为什么ajax post不能加载页面时,使用短url


Why ajax post can not load page when use short url?

为什么ajax post不能在使用短url时加载页面?

当我使用短URL test.php在ajax的帖子像这样

<script>
function send_username_value_for_check_available() {
        $.ajax
        (
            {
                url: 'test.php',
                type: 'POST',
                data: $('#username_send_value_fid').serialize(),
                cache: false,
                success: function (data) {
                    $('#mySpan_username').show();
                    $('#mySpan_username').html(data);
                }
            }
        )
}
</script>

i will get error

XMLHttpRequest cannot load https://example.com/test.php. Response for preflight is invalid (redirect)

但是当我使用完整的URL https://www.example.com/test.php时,它工作得很好。

如何在ajax post中使用短URL ?

我认为这与AJAX与短URK问题无关…

你必须允许CORS从你的服务器为这个域名,它将工作

http://enable-cors.org/server_php.html

您的短URL test.php解析为example.com/test.php
example.comwww.example.com被视为不同的结构域。因此ajax请求被视为跨域调用。
尝试在脚本中使用Access-Control-Allow-Origin头授权example.com和/或www.example.com(您应该避免允许'*')。使用php:

header("Access-Control-Allow-Origin: https://example.com");

更多信息:
-"CORS响应头"可能是搜索关键字,以帮助您在这个主题。
-也看看这个答案:https://stackoverflow.com/a/8689332/3872061