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 :: get fields in object python 
Python :: how to add array and array python 
Python :: python variables 
Python :: is str in pzthon 
Python :: reverse range python 
Python :: flask orm update query 
Python :: remove watermark using python 
Python :: #index operator in python 
Python :: add columns not in place 
Python :: how can you know if a year is a leap year 
Python :: python cat 
Python :: time zone 
Python :: database with python connection 
Python :: extracting values in pandas 
Python :: import file in another path python 
Python :: destory image in pygame 
Python :: python output text 
Python :: python raise filenotfounderror 
Python :: checking length of sets in python 
Python :: extract directory python 
Python :: reverse a list in python 
Python :: how to get all values from class in python 
Python :: strip plot (normal) 
Python :: next power of 2 python 
Python :: add legend to colorbar 
Python :: dataframe select row by index value 
Python :: How to check the number of occurence of each character of a given string in python 
ADD CONTENT
Topic
Content
Source link
Name
4+5 =