Getting HttpResponse in Django from Javascript fetch

Viewed 776

I have a django endpoint:

def endpoint(request):
    return HttpResponse("Hello!")

I call it in JS like this:

fetch(baseUrl + "endpoint", {
      method: "POST",
    }).then(function(data) {
        // HOW DO I ACCESS THE "Hello!"?
    });

How do i access the hello! in the .then part? Thanks!!

1 Answers

Something like this:

fetch(baseUrl + "endpoint", {
      method: "POST",
}).then(function(data) {
        data.text().then( text => console.log(text) );
});
Related