【问题标题】:Cross origin requests are only supported for protocol schemes, what can I do?跨域请求仅支持协议方案,我该怎么办?
【发布时间】:2019-12-05 19:06:01
【问题描述】:

我无法向 php 发送信息,它被阻止了。

跨源请求仅支持以下协议方案:http、data、chrome、chrome-extension、https。

我使用了不同的,只有三分之一的 pc 使代码工作。

$(document).on('ready', function() {
    $('#loginAjax').click(function() {
        $.ajax({
            type: 'POST',
            url: 'prueba.php',
            dataType: 'json',
            data: { myusuario: "hola" },
            success: function(response) {
                $('#result').html(response);
            },
            error: function() {
                $("#result").html("no vas a aprobar");}
        });
    });

    $('#redRegistrar').click(function() {
        location.href = 'registro.html';
    });
});
<?php
header("Access-Control-Allow-Origin: *");
if ($_SERVER["REQUEST_METHOD"] == "POST") {
    $recibido = $_POST['myusuario'];
    echo json_encode( $recibido );
}
?>

我希望它会说hola

【问题讨论】:

  • file:///C:/xampp/htdocs/app_bloqui/prueba.php' from origin 'null' 已被 CORS 策略阻止:跨源请求仅支持协议方案:http,数据,铬,铬扩展,https。
  • 我为你修正了代码格式。

标签: php html json jss


【解决方案1】:

您似乎正试图从file:// URI 访问prueba.php。 这可能是因为您直接在浏览器中打开了一个 HTML 文件,而文件不支持跨源请求。

相反,您应该将带有 javascript 的 HTML 文件放在您的网络服务器中,然后访问该 URL。

例如,从错误中猜测路径,您需要将 HTML 文件移动到 C:/xampp/htdocs/app_bloqui/test.html,然后访问 127.0.0.1/app_bloqui/test.html。这意味着您将位于同一来源并且请求将起作用。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2018-06-29
    • 2018-06-27
    • 1970-01-01
    • 1970-01-01
    • 2018-10-03
    • 2016-07-19
    • 2017-02-22
    • 2017-10-11
    相关资源
    最近更新 更多