Guide - Fetch
Guide - Fetch
Intro
The Fetch API provides a JavaScript interface for accessing and manipulating parts of the HTTP pipeline, such as requests and
responses. It also provides a global fetch() method that provides an easy, logical way to fetch resources asynchronously across the
network.
This kind of functionality was previously achieved using XMLHttpRequest. Fetch provides a better alternative that can be easily
used by other technologies such as Service Workers. Fetch also provides a single logical place to define other HTTP-related
concepts such as CORS and extensions to HTTP.
function request_nse_element(){
let career_credit = {{ credit.id }};
let url = `/personal/credit/${career_credit}/parametrics/nse/`;
let data = {};
fetch(url, {
csrfmiddlewaretoken: "{{ csrf_token }}",
method: 'POST', // or 'PUT'
body: JSON.stringify(data), // data can be `string` or {object}!
headers:{
'Content-Type': 'application/json',
'X-CSRFToken': getCookie('csrftoken')
}
}).then(res => res.json())
.catch(() => {
document.getElementById("parametric_nse").innerHTML = "<p>Hubo un Error</p>";
})
.then(response => {
document.getElementById("parametric_nse").innerHTML = response.content;
});
}