您当前的位置: 首页 > 

壹小俊

暂无认证

  • 3浏览

    0关注

    885博文

    0收益

  • 0浏览

    0点赞

    0打赏

    0留言

私信
关注
热门博文

解决跨域携带Cookie问题(如tp6验证码)

壹小俊 发布时间:2020-07-28 11:33:26 ,浏览量:3

php接口,解决跨域的构造函数

/*
     * 解决跨域,并统一接收参数--析构方法
     */
    protected function _initialize()
    {
        //比其他跨域多了Access-Control-Allow-Credentials,允许携带Cookie
        header('Access-Control-Allow-Credentials: true');
        header('content-type:text/html;charset=utf-8');
        //而且起源不能是通配符'*',必须为具体网址
        header('Access-Control-Allow-Origin: http://localhost:8080');
        header("Access-Control-Allow-Headers: Origin, X-Requested-With, Content-Type, Accept");
        header('Access-Control-Allow-Methods: GET, POST, PUT, DELETE');
    }

也可以在index.php头加

header('Access-Control-Allow-Credentials: true');
header('content-type:application:json;charset=utf8');
//header('Access-Control-Allow-Origin: https://test.easyketang.com');
header('Access-Control-Allow-Origin: http://localhost:8080');
header('Access-Control-Allow-Methods:PUT,POST,GET,OPTIONS,DELETE');
header("Access-Control-Allow-Headers: Origin, X-Requested-With, Content-Type, Accept");
jquery的$.ajax写方法示例
$.ajax({
                type : "POST",
                url : this.apiUrl+"login",
                contentType: "application/json",
                xhrFields: {
                    withCredentials: true
                },
                data: JSON.stringify(this.user),
                success : function(response) {
                    console.log(JSON.parse(response));
                },
                error : function(response){
                    console.log(JSON.parse(response));
                }
            });
关注
打赏
1664335782
查看更多评论
立即登录/注册

微信扫码登录

0.0411s