簡體   English   中英

使用 javascript 從 Django Rest 框架創建和獲取 authtoken

[英]Creating and fetching authtoken with javascript from Django Rest Framework

http POST http://localhost:8000/api-auth-token/ username="saadman" password="Sakib123#"

當我在終端上運行它時它工作正常。

現在,我正在嘗試在 javascript 中實現等效代碼以獲取 authtoken:

fetch('http://localhost:8000/api-auth-token/',{
    method:'POST',
    header:{
    username: 'saadman'
    password: 'Sakib123#'
  }
}

但它不工作

最后這對我有用:

var raw = JSON.stringify({"username":"saadman","password":"Sakib123#"});

var requestOptions = {
  method: 'POST',
  headers:  {"Content-Type": "application/json"},
  body: raw,
  redirect: 'follow'
};

fetch("http://192.168.43.189:8000/api-auth-token/", requestOptions)
  .then(response => response.text())
  .then(result => console.log(result))
  .catch(error => console.log('error', error));

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM