xgqfrms™, xgqfrms® : xgqfrms's offical website of GitHub!

Fetch & POST


    fetch(
        `http://10.1.5.202/deploy/http/send/viewtree`,
        {
            method: "POST",
            mode: "cors",
            headers: {
                "Content-Type": "application/x-www-form-urlencoded",// type
            },
            body: JSON.stringify({"pro_name":"SYDBS.trunk"}),// form url string
        }
    )
    .then(res => res.json())
    .then(
        (json) => {
            console.log(`post json`, json);
            return json;
        }
    )
    .catch(err => console.log(`fetch post error`, err));


image

POST & form query url


const fetchPOSTJSON = (url = ``, queryObj = {}) => {
    let {
        pro_name,
        apis,
        message
    } = queryObj;
    let query = `pro_name=${pro_name}&apis=${JSON.stringify(apis)}&message=${message}`;
    return fetch(url,
        {
            method: "POST",
            mode: "cors",
            headers: {
                "Content-Type": "application/x-www-form-urlencoded",
                // "Content-Type": "application/json",
                // "Content-Type": "application/json; charset=utf-8",
            },
            // body: JSON.stringify(queryObj),// object
            // body: queryObj,// object
            body: query,// string
        })
        .then(res => res.json())
        .then(
            (json) => {
                console.log(`post json`, json);
                return json;
            }
        ).catch(err => console.log(`fetch post error`, err));
};


原文地址:https://www.cnblogs.com/xgqfrms/p/9252386.html