I’m using fetch and then trying to return some json with this: return JsonResponse({"data1": 1, "data2": 2})
In views.py:
@csrf_exempt
def test(request):
if request.method == "PUT":
# this json is not in the response, see comment in JavaScript
# how do I send json so I can use it with fetch and do somethig with the data?
return JsonResponse({"data1": 1, "data2": 2})
else:
# this sends and json is displayed in browser if request is GET
return JsonResponse({"data1": 1, "data2": 2})
In my client.js:
const test = document.getElementById("test")
test.addEventListener("click", runTest)
function runTest(e) {
fetch("/test", {method: "PUT"})
.then( res => console.log(res))
// Response { type: "basic", url: "http://localhost:8000/test", redirected: false, status: 200, ok: true, statusText: "OK", headers: Headers, body: ReadableStream, bodyUsed: false }
.catch( err => console.log(err))
e.preventDefault()
}
How do you send json with Django so you can use the json with Javascript to do things?