duanfu3634 2016-02-05 19:36
浏览 997
已采纳

请求标头字段Access-Control-Allow-Headers在预检响应中不允许使用Access-Control-Allow-Headers

I am trying to make a login page from cross domain but I couldn't solve the problem, the error is:

XMLHttpRequest cannot load http://localhost/testing/resp.php. Request header field Access-Control-Allow-Headers is not allowed by Access-Control-Allow-Headers in preflight response.

My Javascript code is:

$('#login').click(function(){
        var username = $('#uname').val();
        var password = $('#pass').val();
        var result = $('.result');
        result.text('loading....');

        if (username != '' && password !=''){
            var urltopass = 'action=login&username='+username+'&password='+password;
            $.ajax({
                type: 'POST',
                data: urltopass,
                headers: {"Access-Control-Allow-Headers": "Content-Type"},
                url: 'http://localhost/testing/resp.php',
                crossDomain: true,
                cache: false,
                success: function(responseText){
                    console.log(responseText);
                    if(responseText== "0"){
                        result.text('incorrect login information');
                    } else if (responseText == "1"){
                        window.location="http://localhost/testing/home.php";
                    } else{
                        alert('error in sql query 
' + responseText);
                    }
                }
            });
        } else return false;
    });

The PHP code for http://localhost/testing/resp.php :

<?php
    include "db.php"; //Connecting to database

    if (!isset($_SERVER['HTTP_ORIGIN'])) {
        echo "This is not cross-domain request";
    exit;
}
    header("Access-Control-Allow-Origin: *");
    header("Access-Control-Allow-Credentials: true");
    header("Access-Control-Allow-Methods: POST, GET, OPTIONS");
    header("Access-Control-Allow-Headers: Content-Type, Authorization, X-Requested-With");
    header('P3P: CP="CAO PSA OUR"'); // Makes IE to support cookies
    header("Content-Type: application/json; charset=utf-8");

    if (isset($_POST['action']) && $_POST['action'] == 'login'){
        $uname = $_POST['username'];
        $pass = $_POST['password'];

        $sql = "SELECT * FROM loginajax WHERE username='$uname' AND password='$pass'";
    
        $rs=$conn->query($sql);

        if (mysqli_num_rows($rs) <= 0){
            echo "0";
        } else {
            echo "1";
        }
        
    } else echo "this is not Login";

?>

</div>
  • 写回答

1条回答 默认 最新

  • douci4026 2016-02-05 19:54
    关注

    remove this:

    headers: {"Access-Control-Allow-Headers": "Content-Type"},
    

    from your jQuery.ajax call.

    The server responds with a Access-Control-Allow-Headers header, the client doesn't send it to the server.

    The client sends a Access-Control-Request-Headers to request allowing certain headers, the server responds back with with a Access-Control-Allow-Headers that lists the actual headers its going to allow. The client does not get to demand what headers are allowed.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 名为“Product”的列已属于此 DataTable
  • ¥15 安卓adb backup备份应用数据失败
  • ¥15 eclipse运行项目时遇到的问题
  • ¥15 关于#c##的问题:最近需要用CAT工具Trados进行一些开发
  • ¥15 南大pa1 小游戏没有界面,并且报了如下错误,尝试过换显卡驱动,但是好像不行
  • ¥15 没有证书,nginx怎么反向代理到只能接受https的公网网站
  • ¥50 成都蓉城足球俱乐部小程序抢票
  • ¥15 yolov7训练自己的数据集
  • ¥15 esp8266与51单片机连接问题(标签-单片机|关键词-串口)(相关搜索:51单片机|单片机|测试代码)
  • ¥15 电力市场出清matlab yalmip kkt 双层优化问题