【问题标题】:How, can i access tastypie authenticate webservice through ajax如何,我可以通过ajax访问tastepie认证网络服务吗
【发布时间】:2014-12-22 11:32:54
【问题描述】:

我正在尝试通过 ajax 访问下面的 web 服务,但它不起作用。 http://task.woo.gy/api/v1/article/?format=json 用户名: - 根 密码:- rohit

ajax 函数

  $.ajax({
        type: "POST",
        url: "http://task.woo.gy/api/v1/article/?format=json",
        data: "{ 'username': 'root', 'password': 'rohit'}",
        contentType: "application/json; charset=utf-8",
        success: ajaxCallSucceed_allorder,
        dataType: "json",
        failure: ajaxCallFailed_allorder
    });
 function ajaxCallSucceed_allorder()
 {
  alert('success');
 }
 function ajaxCallFailed_allorder()
 {
  alert('failed');
 }

【问题讨论】:

    标签: javascript jquery ajax django tastypie


    【解决方案1】:

    让我们先不要在互联网上以明文形式发送您的用户名和密码。 Tastypie 有一个 API 密钥认证机制,允许您使用用户和 API 密钥(无密码)登录。有一种方法可以为新用户自动创建 API 密钥,我相信有一个管理命令可以为现有用户创建密钥。

    关于 Ajax 调用。在 url 中将用户名和密钥作为获取参数传递或使用授权标头。对于后一个选项,您可以使用:

    beforeSend: function (xhr) {
        xhr.setRequestHeader ("Authorization", "Authorization: ApiKey <username>:<api_key>");
    
    },
    

    混合:https://stackoverflow.com/a/5507289How do I set the authorization header for tastypie?

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2022-11-30
      • 1970-01-01
      • 2011-10-25
      • 2016-04-01
      • 2014-04-23
      • 1970-01-01
      • 2013-03-20
      相关资源
      最近更新 更多