【发布时间】:2020-11-04 23:36:16
【问题描述】:
我正在开发一个 React Native 应用程序。作为后端,我使用的是 DJango DRF。我正在尝试发出 POST 请求以在后端创建新元素,这是我在 React 中的代码:
**API.JS**
const routes = {
accounts: {
get: () =>
requestHelper({
method: "get",
url: "accounts/",
}),
post: (data) =>
requestHelper({
data,
method: "post",
url: "accounts/",
}),
},
};
**API CALL**
const formData = new FormData();
const image = {
uri: data.image,
name: data.timestamp + ".jpg",
type: "image/jpeg",
};
_.map(data, (item, name) => {
formData.append(name, item);
});
formData.append("image", image);
await api.accounts
.post(formData)
.then((res) => {
console.log(res, "OK");
})
.catch((err) => {
console.log(err);
});
};
请求正在到达后端,并且正在数据库(包括图像)上创建新帐户。问题是,尽管 Django 返回 200_OK,但 api 调用将转到 catch 语句,并且控制台上出现此错误:
网络错误
堆栈跟踪:node_modules/axios/lib/core/createError.js:15:0 in node_modules/axios/lib/adapters/xhr.js:81:4 在 dispatchXhrRequest
node_modules/event-target-shim/dist/event-target-shim.js:818:20 在 EventTarget.prototype.dispatchEvent
node_modules/react-native/Libraries/Network/XMLHttpRequest.js:575:10 在 setReadyState
node_modules/react-native/Libraries/Network/XMLHttpRequest.js:389:6 in __didCompleteResponse node_modules/react-native/Libraries/vendor/emitter/EventEmitter.js:189:10 在发射
node_modules/react-native/Libraries/BatchedBridge/MessageQueue.js:425:19 在 __callFunction
node_modules/react-native/Libraries/BatchedBridge/MessageQueue.js:112:6 在 __guard$argument_0
node_modules/react-native/Libraries/BatchedBridge/MessageQueue.js:373:10 在 __guard
node_modules/react-native/Libraries/BatchedBridge/MessageQueue.js:111:4 在 callFunctionReturnFlushedQueue [本机代码]:null in callFunctionReturnFlushedQueue
我认为不是图像问题,因为我已删除以进行测试并出现相同的错误。
【问题讨论】:
标签: python reactjs django react-native django-rest-framework