Multipart body must have at least one part

使用RN出这个错误,经检查有问题的代码是:

    postJson(cmd, dataObj)
    {
        let url = Defines.SERVER_ADDR + cmd;
        let formData = new FormData();
        let headers ={};
        for (let eachKey in dataObj)
        {
            formData.append(eachKey, dataObj[eachKey]);
        }
        console.log("[REQ]",url,dataObj,headers);
        return fetch(url, {method: "post", body: formData, headers }).then(
            function (response)
            {
                if (response.status !== 200)
                {
                    throw new Error("服务器状态不正常:" + response.status);
                }
                let jsonRet = response.json();
                console.log("[RESP]",jsonRet);
                return jsonRet;
            }
        );
    }

被调用来发送get的请求,传入的dataObj是{},而FormData至少有一个字段才行。如果没有post的字段,就不要发送FormData。

    getJson(cmd)
    {
        let url = Defines.SERVER_ADDR + cmd;
        let headers ={};
        console.log("[REQ]",url,headers);
        return fetch(url, {method: "get", headers }).then(
            function (response)
            {
                if (response.status !== 200)
                {
                    throw new Error("服务器状态不正常:" + response.status);
                }
                let jsonRet = response.json();
                console.log("[RESP]",jsonRet);
                return jsonRet;
            }
        );
    }

技术内容转载请注明来源,个人日记不允许转载,欢迎指出任何有错误或不够清晰的表达。可以邮件至 mousebomb@gmail.com