2014-10-27 23 views
0

我試圖通過ajax訪問以下webservice,但它不工作。 http://task.woo.gy/api/v1/article/?format=json 用戶名: - 根 密碼: - 羅希特如何,我可以通過ajax訪問tastypie驗證webservice

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'); 
} 

回答

0

讓我們開始通過互聯網明文不發送用戶名和密碼。 Tastypie有一個API密鑰認證機制,允許您使用用戶和API密鑰(無密碼)登錄。有一種方法可以爲新用戶自動創建一個API密鑰,我相信有一個管理命令可以爲現有用戶創建密鑰。

至於Ajax調用。請將用戶名和密鑰作爲get參數傳遞給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?