跨源请求仅支持协议方案,我该怎么办? [英] Cross origin requests are only supported for protocol schemes, what can I do?

查看:47
本文介绍了跨源请求仅支持协议方案,我该怎么办?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我无法将信息发送到php,它被阻止了.

I can't send info to php, it gets blocked.

跨源请求仅支持以下协议方案:http,数据,chrome,chrome扩展名,https.

Cross origin requests are only supported for protocol schemes: http, data, chrome, chrome-extension, https.

我使用了不同的代码,只有三分之二的代码使代码正常工作.

I used different and just 1 out of 3 pc makes the code work.

$(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:// URI访问 prueba.php .这可能是因为您直接在浏览器中打开了HTML文件,并且文件不支持跨源请求.

It look like you are trying to access the prueba.php from a file:// URI. This is likely because you opened a HTML file directly in your browser, and cross origin requests are not supported for files.

相反,您应该将HTML文件和javascript放到Web服务器中,然后访问URL.

Instead, you should place your HTML file with your javascript inside your web server, and then visit the URL.

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

For example, guessing paths from your error, you need to make move your HTML file to C:/xampp/htdocs/app_bloqui/test.html and then visit 127.0.0.1/app_bloqui/test.html. This will mean that you will be on the same origin and the request will work.

这篇关于跨源请求仅支持协议方案,我该怎么办?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆