使用RN出这个错误,经检查有问题的代码是:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 |
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。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 |
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; } ); } |