简体   繁体   中英

Django is returning JsonResponse, can see the desired JSON response in browser Network tab, but unable to access on the FE

Not quite sure what I'm doing wrong here.

I have a method in utils.py that returns the dictionary:

import boto3
import base64


# Create a Rekognition client
def detect_faces(photo):
    ...
    return {
        'comment': comment,
        'rekognition_response': response,
        'url': url,
    }

Which sent back to the views.py :

from django.shortcuts import render
from django.http import JsonResponse, HttpResponse

from . import utils

def index(request):
    return render(request, 'index.html')

def submit(request):
    response = utils.detect_faces(request.body)
    return JsonResponse(response)

I can see the response I'm expecting in the Network tab of the browser:

在此处输入图像描述

But then on FE, I'm not seeing it in the res :

const submitScreenshot = async () => {
  const picture = document.getElementById('webcam-picture-submit').src;
  const res = await fetch('/submit/',
  {
    method: 'POST',
    headers: {
      'X-CSRFToken': csrftoken 
    },
    body: picture.replace('data:image/png;base64,','')
  })
  console.log(res);
  console.log('Submitted.');
};

在此处输入图像描述

I get the sense this is something super basic I'm missing, what is it?

As documented you have to receive data from Response which is also Promise

The fetch() method takes one mandatory argument, the path to the resource you want to fetch. It returns a Promise that resolves to the Response to that request, whether it is successful or not

fetch('http://example.com/movies.json').then(response => response.json()).then(data => console.log(data));

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM