django 接受 js 傳遞的 POST參數

第已種情況(後端接收到的參數,需要自己解析)

前端js代碼,傳參方式

async function createCustomer(method, card) {
    var para= {
		  items: card,
		  currency: "method"
		};
    return fetch('/create/', {
        method: 'post',
        body: JSON.stringify(orderData),
        mode: 'cors',
        cache: 'default',
        credentials: 'include',
    })
        .then(response => {
            return response.json();
        })
        .then(subscription => {
            next(subscription);
        });
}

後端接受參數

import json

def sb(request):
	"""
	需要自己轉成json格式
	"""
	para = json.loads(request.body.decode())
	pass

第二種情況(後端接收到的參數,有框架自動解析)

前端js代碼,傳參方式

async function createCustomer(paymentMethod, cardholderEmail) {
    var formData = new FormData();
    formData.append('email', cardholderEmail);
    formData.append('payment_method', paymentMethod);
    formData.append('csrfmiddlewaretoken', csrftoken);
    return fetch('/create-customer/', {
        method: 'post',
        // headers: {
        //     'Content-Type': 'application/json'
        // },
        body: formData,
        mode: 'cors',
        cache: 'default',
        credentials: 'include',
    })
        .then(response => {
            return response.json();
        })
        .then(subscription => {
            handleSubscription(subscription);
        });
}

後端接受參數

def sb(request):
	"""
	框架自動解析參數
	"""
	para = request.POST
	pass
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章