Search
 
SCRIPT & CODE EXAMPLE
 

JAVASCRIPT

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 :: v-select on change 
Javascript :: email validation in react js 
Javascript :: react router Link does work 
Javascript :: Chart is not a constructor 
Javascript :: how to pass an object directly to formdata in javascript 
Javascript :: set year in javascript 
Javascript :: js remove spaces 
Javascript :: javascript loop through object example 
Javascript :: How to get unix timestamp from tomorrow nodejs 
Javascript :: retrieve object array value based on key 
Javascript :: como actualizar node en windows 
Javascript :: javascript replace string 
Javascript :: An accessor cannot be declared in an ambient context. 
Javascript :: How to change htm h1 from nodejs 
Javascript :: placeholder in angular 9 select with working required 
Javascript :: regex for yyyy-mm-dd 
Javascript :: socket.io.js file download 
Javascript :: iframe in angular 
Javascript :: nuxt small scroll 
Javascript :: how to get the nth child of dom js 
Javascript :: .style.display 
Javascript :: jquery set value by id 
Javascript :: react native curved view 
Javascript :: neo4j delete relationship nodes 
Javascript :: instantiate object in script godot 
Javascript :: asyncstorage react native 
Javascript :: remove after js 
Javascript :: html javascript redirect 
Javascript :: backgroundcolor react 
Javascript :: cypress set viewport for all test cases 
ADD CONTENT
Topic
Content
Source link
Name
3+4 =