Getting Type error Failed to fetch in fetch Api

  django, javascript, server

I am getting an error in fetch API while calling the django server.
The request used here is:

async function analyse() {
await fetch("http://localhost:8000/classify/", {
    method: 'POST',
    body: JSON.stringify({
        premise: document.getElementById('pre').value,
        hypothesis: document.getElementById('hypo').value,
    }),
    headers: {
        "Content-Type": "application/json; charset=UTF-8",
        "Authorization":"Basic c2hhc2h2YXQ6cGFuZGV5"
    }
    }).then(function (response) {
        console.log(response);
        return response.json()
    }).then(function (text) {
        console.log(text);
    }).catch(err => {
        console.log(err)
    })

It is returning an error with Type Error.
The request is getting to the django server and the response is printed to the terminal correctly. But it is not returning the response to the browser.

def post(self, request):
print(request.data)
print(request.data["premise"])
print(request.data["hypothesis"])

data = [[request.data["premise"], request.data["hypothesis"]], ['i am good', 'I am great']]
temp_data = pd.DataFrame(data, columns=['premise', 'hypothesis'])
temp_dataset = MyDataset(temp_data, PredictorConfig.tokenizer, labels=False)

temp_dataloader = DataLoader(dataset=temp_dataset,
                             sampler=BatchSampler(
                                 SequentialSampler(temp_dataset),
                                 batch_size=8, drop_last=False), shuffle=False)
temp_preds = submission_predict(PredictorConfig.model, temp_dataloader, PredictorConfig.device)
print(temp_preds[0])
return JsonResponse({"result":temp_preds[0]})

What to do here?

Source: Ask Javascript Questions

LEAVE A COMMENT