javascript - 从 promise 对象中获取数据
问题描述
我目前处于需要从 Promise 对象中获取数据以便通过 Vue 更新我的视图的情况。但是我似乎无法从这个承诺对象中获取数据?有谁知道我该怎么做才能从 JSONresponse 访问数据。
请注意,我已发出 POST 请求以创建新的“模块”,现在我需要将这些值传回以更新我的视图。
请在下面找到:
async createModule() {
let response = await fetch('create_module', {
method: 'POST', // Method itself
headers: {
"contentType": "application/json",
"X-CSRFToken": document.querySelector("[name=csrfmiddlewaretoken]").value,
},
body: JSON.stringify({
code: document.getElementById('code').value,
module_name: document.getElementById('module_name').value,
max_students: document.getElementById('max_students').value,
start_date: document.getElementById('start_date').value,
}) // We send data in JSON format
});
if (response.ok) {
// module was created
//this.modules.push(data);
console.log(response.json()); // HERE IS WHERE I AM TRYING TO ACCESS ALL THE VALUES FROM THE RESPONSE
} else {
alert("failed to create module");
}
}
我的 API:
def create_module(request):
"""API for creating a module"""
if request.method == "POST":
PUT = json.loads(request.body)
module = Module(module_name=PUT['module_name'], code=PUT['code'], max_students=PUT['max_students'], start_date=PUT['start_date'])
module.save()
return JsonResponse({'module_name': PUT['module_name'], 'code': PUT['code'], 'max_students': PUT['max_students'], 'start_date': PUT['start_date']})
return HttpResponseBadRequest("Invalid method")
感谢您的时间。
解决方案
推荐阅读
- java - 流口水在一段时间内没有达到规则
- python-3.x - 在django中从oracle数据库获取数据到postgresql
- object-detection - (Faster R-CNN)ROI 池化层在框坐标中不可微
- amazon-web-services - 使用 AWS Lambda 为 S3 静态网站创建动态联系表单时添加目的地时面临的问题
- python - 类型错误:onLocation() 缺少 1 个必需的位置参数:“实例”
- vue.js - Vue - 何时使用功能组件?
- c++ - 当我期待 R 中的数据帧输出时,为什么 Rccp 会返回类似列表的输出?
- c - 通用 void* 说明对成员“单词”的请求不是结构或联合
- html - 尝试使用一些 CSS 在 DIV 中定位 UL(虽然我无法创建类)
- batch-file - 将命名参数传递给带有特殊字符的批处理脚本