Search
 
SCRIPT & CODE EXAMPLE
 

PYTHON

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
Python :: python if file exists append else create 
Python :: parse invoice python 
Python :: python dataframe show row 
:: python remove first item in list 
Python ::  
Python :: drf model methods serializer 
Python :: download video to from pytube with a special name 
::  
:: Broadcasting with NumPy Arrays Two dimension array dimension array Example 
Python :: django form 
:: convert pandas data frame to latex file 
Python :: how to take n space separated input in python” Code Answer’s 
Python :: reversing in python 
Python ::  
:: python typing module list 
::  
Python :: Python DateTime Date Class Example 
Python :: correlation with target variable python 
Python :: python console 
::  
::  
Python ::  
Python :: expand pandas dataframe into separate rows 
Python ::  
Python :: smooth interpolation python 
Python :: list slice in python 
Python ::  
:: df loc 
:: double underscore methods python 
Python :: are logN and (lognN) same 
ADD CONTENT
Topic
Content
Source link
Name
5+3 =