1 回答

TA貢獻(xiàn)1804條經(jīng)驗(yàn) 獲得超2個贊
我們開工吧。
這是您在 javascript 中的字典:
var data = {"name":"nitin raturi"};
現(xiàn)在定義一個使用 jquery 發(fā)出 post 請求的函數(shù)。
function send_data(data, callback) {
// callback is a function that you should pass to handle your response
$.ajax({
type: "POST",
url: '/sample-url/', // change this to your url here
data: {"data": JSON.stringify(data)},
success: function (response) {
callback(response);
},
});
}
現(xiàn)在像這樣使用 send_data 函數(shù):
send_data(data,function(response){
//handle your response here
console.log(response);
})
現(xiàn)在您可以像這樣在 django 視圖中訪問您的數(shù)據(jù):
import json
@csrf_exempt
def my_view(request):
data = request.POST.get('data')
data = json.loads(data) # this will convert your json data to python dictionary
name = data.get("name")
// Handle your data and return anything you wish
return render(request,"index.html",{})
添加回答
舉報