使用Javascript访问Django Rest Framework API - 获取令牌 [英] Access Django Rest Framework API using Javascript - getting a Token

查看:62
本文介绍了使用Javascript访问Django Rest Framework API - 获取令牌的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用Django Rest Framework进行API设置。通过cURL或HTTPie甚至是可浏览的API访问时,它工作正常。 API具有令牌身份验证,因此最初您必须提供将返回令牌的凭据。使用HTTPie(甚至curl)你会这样做:

I have an API setup using Django Rest Framework. It works fine when accessing via cURL or HTTPie or even the browsable API. The API has token authentication so initially you have to supply credentials which will return a token. Using HTTPie (or even curl) you would do this:

http POST http://127.0.0.1:8000/api/v1/api-token-auth/ username="user1" password="testpassword"

这将返回回复例如:

HTTP/1.0 200 OK
Allow: POST, OPTIONS
Content-Type: application/json
Date: Sun, 03 Sep 2017 16:57:38 GMT
Server: WSGIServer/0.2 CPython/3.6.1
X-Frame-Options: SAMEORIGIN

{
    "token": "fgfdgfdgfdgfdgd45345345lkjlj"
}

你然后将获取令牌并执行GET / PUSH /等,如下所示:

You would then take the token and perform a GET/PUSH/etc like so:

http --json POST http://127.0.0.1:8000/api/v1/test/ test_text="Testing" 'Authorization: Token fgfdgfdgfdgfdgd45345345lkjlj'

我已经谷歌搜索了一段时间,并没有找到任何明确的答案,如何将上述两行转换为Javascript?我如何(1)通过凭证获取令牌; (2)检索令牌; (3)使用令牌发出GET和PUSH请求?

I have been Google searching for a while now and cannot find any clear answers as to how the above two lines would translate into Javascript? How do I (1) Pass through credentials to get a token; (2) Retrieve the Token; (3) Use the token to make a GET and PUSH request?

推荐答案

我同意你应该使用Ajax。

I agree you should use Ajax.

您需要在应用程序的最开始时调用ajax:

You need an ajax call in the very beginning of you app:

var data = {username:'user',password:'password'}

$.ajax({
       type: 'POST',
       data: data,
       url: 'http://your_url',
       success: function(res){
               console.log(res)
               $.ajaxSetup({
                  headers: {
                    "token": res.token
                  }
               });
       },
       error: function(error) {
           callbackErr(error,self)
       }
   })

Haven`t经过测试,但想法是使用Ajax调用获取令牌并使用.ajaxSetup将令牌保存到所有后续ajax请求的标头。

Haven`t tested, but idea is use an Ajax call to get the token and use .ajaxSetup to save the token to a header for all following ajax requests.

你可以这样做:

var data = {test_text="Testing"}
$.ajax({
           type: 'POST',
           data: data,
           url: 'http://127.0.0.1:8000/api/v1/test/',
           success: function(res){
                   console.log(res)  //answer of api call.
                   });
           },
           error: function(error) {
               callbackErr(error,self)
           }
       })

或者这个:

$.ajax({
           type: 'GET',
           url: 'http://127.0.0.1:8000/api/v1/ANOTHER_TES/',
           success: function(res){
                   console.log(res)  //answer of api call.
                   });
           },
           error: function(error) {
               callbackErr(error,self)
           }
       })

更改键入调用参数以更改您的请求。

Change type parameter of the call to change your request.

这篇关于使用Javascript访问Django Rest Framework API - 获取令牌的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆