鍍金池/ 問答/PHP  HTML/ 怎么給jquery跨域post請求添加頭部信息?

怎么給jquery跨域post請求添加頭部信息?

跨域,服務(wù)器已開放請求接口,用$post可以成功,但是無法添加header信息,用$ajax的話會把post信息換成OPTIONS發(fā)送請求,data跟header都無法發(fā)送出去,不知道有什么好的辦法可以解決~~

//        $.post(Common.postUrl + '/wechat/register',
//                JSON.stringify({
//                    phone: tel,
//                    publicId: getcookie().publicId,
//                    header: header,
//                    authCode: code,
//                    focusTime: Date.parse(new Date())
//                }), function (data) {
//                    data = JSON.parse(data);
//                    if (data.status == 200) {
//                        alertBottom('登錄成功');
//                        setTimeout(function () {
//                            window.href = 'index.html';
//                        }, 2000)
//                    } else {
//                        alertBottom(returnMessage[data.status]);
//                    }
//                });
   $.ajax({
            type: "POST",
            url: Common.postUrl + '/wechat/register',
            data:JSON.stringify({
                phone: tel,
                publicId: getcookie().publicId,
                header: header,
                authCode: code,
                focusTime: Date.parse(new Date())
            }),
            contentType: "application/x-www-form-urlencoded",
            headers:{
                key:'Cookie'
            },
            processData: false,
            beforeSend: function (xhr) {
                xhr.setRequestHeader("key", "Cookie");
            },
            success: function (data) {
               
            }
        });
回答
編輯回答
孤巷

你好!我想問下,你那個(gè)ajax跨域請求是如何把請求頭發(fā)過去的,在后臺怎么設(shè)置的。謝謝

2017年8月18日 23:57
編輯回答
遲月

用$ajax的話會把post信息換成OPTIONS發(fā)送請求,data跟header都無法發(fā)送出去。。。。。。。是因?yàn)槟阕远x了頭部,所以自然會從簡單請求變?yōu)轭A(yù)檢請求(Option),這種只有服務(wù)端多做一次處理,可參考文章

2017年4月2日 15:24
編輯回答
疚幼

身邊發(fā)生的那

2018年4月23日 06:26
編輯回答
吢涼

還沒有找到原因,有沒有大佬能夠給點(diǎn)建議啊~~

2018年8月21日 03:51
編輯回答
空痕
 $.ajax({
    type: "POST",
    url: Common.postUrl + '/wechat/register?'+
    data:{
        phone: tel,
        publicId: getcookie().publicId,
        header: header,
        authCode: code,
        focusTime: Date.parse(new Date())
    },
    contentType: "application/x-www-form-urlencoded",
    headers:{
        key:'Cookie'
    },
    processData: false,
    beforeSend: function (xhr) {
        xhr.setRequestHeader("key", "Cookie");
    },
    success: function (data) {
       
    }
});
2017年8月25日 07:46