Jquery PHP跨域問題自動注入cookie

客戶端代碼
domain:h5.html.com

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Title</title>
</head>
<body>
<div><button id="orderList">訂單列表</button></div>
<div>
    <button id="login">登錄</button>
</div>
<div>
    <button id="setClientType">客戶端類型</button>
</div>
<div>
    <!--<form action="http://qilin.t.isjue.cn/topay/order/qlpay">-->
        <!--<input type="submit" value="支付"/>-->
    <!--</form>-->
    <a href="test_pay.html">去支付</a>
</div>
<script src="res/common/js/jquery.js"></script>
<script>
    var dom = {};
    dom.login = function () {
        $.ajax({
            url:"http://qilin.t.isjue.cn/api/login/index",
            xhrFields: {
                withCredentials: true
            },
            type:"POST",
            data:{
                identity:"18879170056",
                password:"123456"
            },
            dataType:"json",
            success:function (res) {
                console.log(res)
            }
        });
    };

    dom.setClientType = function () {
        $.ajax({
            url:"http://qilin.t.isjue.cn/api/common/setClientType?type=MALL_MOBILE",
            xhrFields: {
                withCredentials: true
            },
            type:"GET",
            dataType:"json",
            success:function (res) {
                console.log(res)
            }
        });
    };

    dom.getOrderList = function () {
        $.ajax({
            url:"http://qilin.t.isjue.cn/sales/order/getListV2?page=1&type=MALL_ORDER%2CMALL_OVERSEAS%2CMALL_SHOPKEEPER&pagesize=10&isSalesMode=3&isIncludeProduct=1",
            xhrFields: {
                withCredentials: true
            },
            type:"GET",
            dataType:"json",
            success:function (res) {
                console.log(res);
            }
        });
    };

    $(document).ready(function () {
        // $.ajaxSetup({xhrFiled:{
        //         'withCredentials':true
        //     }});
        dom.setClientType();
        $("#login").on("click",function () {
            dom.login();
        });
        $("#setClientType").on("click",function () {
            dom.setClientType();
        });
        $("#orderList").on("click",function () {
            dom.getOrderList();
        });
    });
</script>
</body>
</html>
php 支持跨域
<?php
header('Access-Control-Allow-Credentials: true');
header('Access-Control-Allow-Origin: http://h5.html.com');
header('Access-Control-Allow-Methods: POST,GET,PUT,DELETE,OPTIONS');
header('Access-Control-Max-Age: 600');
關鍵點
 xhrFields: {
    withCredentials: true
}

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章