【发布时间】:2017-12-31 15:59:48
【问题描述】:
我正在尝试使用 javascript fetch 将数据发布到我的 django rest 框架。我已经为我的模型设置并喜欢了 DRF,并从后端设置了所有内容。我使用邮递员应用程序来确保“get”、“post”等都作为实习生工作。但是,现在我在使用 javascript 发布数据以使用 fetch 发布数据时遇到问题。我想将数据添加到我的文章模型中,该模型具有“标题”、“标签”、“内容”、“背景图像”和发布文章的用户。
这就是我的代码的样子
data = JSON.stringify({
headline: "Testing",
tag: "Testing",
background_image: "Testing",
content: "Testing",
user: 1
})
fetch(
"http://127.0.0.1:8000/api/v1/articlesapi/",
{
method: "post",
headers: {"Authorization":"Token ed73db9bf18f3c3067be926d5ab64cec9bcb9c5e"},
body: data
}
).then(function(response) {
return response.json();
}).then(function(data) {
console.log("Data is ok", data);
}).catch(function(ex) {
console.log("parsing failed", ex);
})
但是,我不断收到错误 parsing failed TypeError: Failed to fetch。有谁知道我做错了什么?
【问题讨论】:
标签: javascript django rest api django-rest-framework