js通过ajax发送header信息php接收

一、js:

  1. 要点:
    js通过ajax发送header信息php接收
  2. 完整代码:
    <!DOCTYPE html>
    <html lang="en">
        <head>
            <meta charset="UTF-8">
            <title>Document</title>
            <script src="jquery.min.js"></script>
        </head>
        <body>
                <script>
                        $.ajax({
                                'url':'http://localhost/index.php',
                                'data':{'sex':'male'},
                                'beforeSend':function(xhr){
                                    xhr.setRequestHeader('name','lee');
                                    xhr.setRequestHeader('sex','male');
                                    xhr.setRequestHeader('height','168');
                                },
                                'success':function(data){
                                    console.log(data);
                                }
                            });
                </script>
        </body>
    </html>

    二、php:

  3. 要点:
    js通过ajax发送header信息php接收
  4. 完整代码:
    <?php
            header('Access-Control-Allow-Origin:*');
            header('Access-Control-Allow-Credentials: true');
            header('Access-Control-Allow-Methods:GET,PUT,POST,DELETE,OPTIONS');
            header('Access-Control-Allow-Headers:WWW-Authenticate,Authorization,Set-Cookie,X-Requested-With, Accept, Accept-Version, Content-Length, Content-MD5, Content-Type, Date, X-Api-Version,name,sex,height');
            $header = apache_request_headers();
            var_Dump($header);exit;
上一篇:MySQL 8.0 Invisible Indexes 和 RDS 5.6 Invisible Indexes介绍


下一篇:PHP发送POST和GET请求