【问题标题】:Why ajax post can not load page when use short url?为什么使用短网址时ajax post无法加载页面?
【发布时间】:2016-10-19 12:55:54
【问题描述】:

为什么ajax post使用短url时无法加载页面?

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

<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>

我会出错

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

但是当我使用完整的 URL https://www.example.com/test.php 时效果很好。

如何在 ajax 帖子中使用短网址?

【问题讨论】:

  • preflight - 让它看起来跨原点......多么奇怪
  • 我可以用 .htaccess 解决这个问题吗
  • 页面./test.php 是否正在重定向到其他页面?

标签: javascript php jquery ajax .htaccess


【解决方案1】:

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

您必须允许来自您的服务器的 CORS 用于此域,它才会起作用

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

【讨论】:

    【解决方案2】:

    您的短 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

    【讨论】:

      猜你喜欢
      • 2017-06-22
      • 2012-11-12
      • 1970-01-01
      • 1970-01-01
      • 2019-11-29
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多