react中fetch請求的使用封裝(2)——使用proxy代理解決跨域問題

1、使用proxy解決fetch中的跨域問題

①、在package.json中進行配置

"proxy": "http://192.168.0.162:8012"

②、請求時直接寫地址

2、對fetch的post請求application/json格式

global.$ = {
    post: (info) => {
        fetch(info.url, {
            method: "POST",
            mode: "same-origin",
            headers: {
                // "Content-Type": "application/x-www-form-urlencoded",
                "Content-Type": "application/json;charset=UTF-8",
            },
            body: JSON.stringify(info.body)
        }).then(resp => {
            return resp.json()
        }).then((res) => {
            info.success(res);
        }).catch(function (e) {
            info.error(e);
        });
    }
}

 

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