Search
 
SCRIPT & CODE EXAMPLE
 

JAVASCRIPT

Send Data Using Fetch With Then Syntax


	fetch('/test', {method:"POST", body:JSON.stringify({name:"NAME"}), headers: {'Content-type': 'application/json; charset=UTF-8'}})
    .then(response => response.json())
    .then(data => console.log(data.name));
/*console log 'Name'*/
Comment

Send Data Using Fetch

 
async function sendMe()
{

let r =await fetch('/test', {method: 'POST', body: JSON.stringify({a:"aaaaa"}), headers: {'Content-type': 'application/json; charset=UTF-8'}})
let res = await r.json();
console.log(res["a"]);
}
 
Comment

Send Fetch Post With Data Using Body

#the template in templates/??

function getCookie(name) {
        let cookieValue = null;
        if (document.cookie && document.cookie !== '') {
            const cookies = document.cookie.split(';');
            for (let i = 0; i < cookies.length; i++) {
                const cookie = cookies[i].trim();
                // Does this cookie string begin with the name we want?
                if (cookie.substring(0, name.length + 1) === (name + '=')) {
                    cookieValue = decodeURIComponent(cookie.substring(name.length + 1));
                    break;
                }
            }
        }
        return cookieValue;
    }
    const csrftoken = getCookie('csrftoken');


async    function send()
    {

      
        
let r =      await fetch('/blog/third', {method: 'POST', credentials: 'same-origin', headers:{'Accept': 'application/json',    'X-Requested-With': 'XMLHttpRequest', 'X-CSRFToken': csrftoken}, body: JSON.stringify({'post_data':'Data to post'})});
let res = await r.json();
console.log(res.my_data);

    }

#the view in views/???
#the key difference between express is import json, json.load(request)['the key/name of the data], then create your own json and JsonResponse it back

import json

#.......


def third(request):
    if request.method == "POST":
        data_from_post = json.load(request)['post_data']
        data = {
                'my_data': data_from_post
        }
        
        return JsonResponse(data)
   
  
    
Comment

PREVIOUS NEXT
Code Example
Javascript :: js change classlist 
Javascript :: javascript basic function 
Javascript :: set time in javascript 
Javascript :: range of numbers in javascript 
Javascript :: chart js x axis data bar 
Javascript :: javascript classlist to array 
Javascript :: array check in javascript 
Javascript :: salvar no localStorage react 
Javascript :: js origin without port 
Javascript :: material ui icons next js 
Javascript :: Return the highest number in Arrays in JavaScript 
Javascript :: leaflet dark mode 
Javascript :: nodejs append to json 
Javascript :: threemeshphonematerial url image three js 
Javascript :: line break in js 
Javascript :: react native diasble view 
Javascript :: json to pdf javascript 
Javascript :: kafka nodejs example 
Javascript :: react materialize cdn 
Javascript :: javascript wait for function to finish 
Javascript :: remove suffix string js 
Javascript :: Relative Time momentjs 
Javascript :: sort numbers in array in js 
Javascript :: math.round in javascript 
Javascript :: prepen an element js 
Javascript :: javascript set style attribute 
Javascript :: import all images from folder react 
Javascript :: how to create a object in javascript 
Javascript :: javascript clone object 
Javascript :: byte to integer js 
ADD CONTENT
Topic
Content
Source link
Name
6+9 =