0

i'm trying to access below webservice through ajax, but it's not working. http://task.woo.gy/api/v1/article/?format=json username :- root password :- rohit

ajax function

  $.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');
 }
Rohit Khurana
  • 269
  • 3
  • 8
  • 18

1 Answers1

0

Lets start with not sending your username and password in clear text over the internet. Tastypie has an API key authentication mechanism which allows you to log in with user and API key (no password). There is a way to automatically create an API key for a new user, and I believe there is a management command to create keys for existing users.

As for the Ajax call. Either pass the username and key as get parameters in the url or use an authorization header. For the latter option you can use:

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

},

mix of: https://stackoverflow.com/a/5507289 and How do I set the authorization header for tastypie?

Community
  • 1
  • 1
rrmoelker
  • 152
  • 10