2015-05-23 55 views
0

我能夠在命令行上使用以下命令來使用REST服務。curl的REST調用相當於jQuery AJAX

捲曲-v --user UNAME:通-H 「接受:應用程序/ XML」 http://xyz.abc.def

什麼是AJAX等價的一樣嗎?如何在ajax調用中提供用戶名:密碼?我不UNAME當前功能:密碼是:

function getData() { 
 
    var name, code; 
 
    var serviceURL = "http://xyz.abc.def"; 
 
    $.support.cors = true; 
 
    $.ajax({ 
 
    type: "GET", 
 
    dataType: "xml", 
 
    crossDomain: true, 
 
    url: serviceURL, 
 
    success: function(data) { 
 
     $(data).find("VariableTree Variable").each(function() { 
 
     code = $(this).find("Code").text(); 
 
     name = $(this).find("Name").text(); 
 

 
     $("#variable").append("<option data-value='" + code + "'>" + name + "</option>") 
 
     }); 
 
    }, 
 
    error: function(XMLHttpRequest, textStatus, errorThrown) { 
 
     console.log(errorThrown); 
 
    } 
 
    }); 
 
}

PS:服務是由Spring Security的

+0

如果服務使用sprint安全保護,那麼您在curl命令中使用basic auth,否則我不會看到curl命令是如何工作的。 – abc123

+0

您需要了解[基本認證](http://en.wikipedia.org/wiki/Basic_access_authentication)。這是cURL的默認auth模式類型。查看cURL請求的請求標頭。你會看到'授權:基本sdfdfkhdfkasdfhsdiu'這是你需要你的請求相同的標題。 'sdfdfkhdfkasdfhsdiu'是'user:pass'的base64編碼。 –

+0

-user uname:pass做基本認證。 – Forkmohit

回答

3

讓我們打破curl命令:

捲曲-v --user UNAME:通-H 「接受:應用程序/ XML」 http://xyz.abc.def

-v是冗長的,在阿賈克斯不需要

--user是純文本的用戶身份驗證,但它被髮送作爲基本認證

-H「接受:應用/ xml」的是添加報頭「接受:應用/ XML

在jQuery的阿賈克斯複製這一點,我們需要做的幾件事情:

  1. 發送用戶名和密碼,在基本認證頭
  2. 接受XML作爲返回的數據類型
  3. 發送AJAX請求作爲GET因爲這是捲曲

// this function takes a username and password 
 
function make_base_auth(user, password) { 
 
    var tok = user + ':' + pass; 
 
    // Base64 encoding is what is used by basic auth let's encode our username:password 
 
    var hash = Base64.encode(tok); 
 
    // Let's return the auth header 
 
    return "Basic " + hash; 
 
} 
 

 
// let's get the data 
 
function getData() { 
 
    var name, code; 
 
    var serviceURL = "http://xyz.abc.def"; 
 
    // getting the auth header value 
 
    var basicAuth = make_base_auth(uname, pass); 
 
    $.support.cors = true; 
 
    $.ajax({ 
 
    // using http get as the command type 
 
    type: "GET", 
 
    // accepting xml as the return type 
 
    dataType: "xml", 
 
    crossDomain: true, 
 
    url: serviceURL, 
 
    // setting the basic auth header 
 
    beforeSend: function(xhr) { 
 
     xhr.setRequestHeader("Authorization", basicAuth); 
 
    }, 
 
    success: function(data) { 
 
     $(data).find("VariableTree Variable").each(function() { 
 
     code = $(this).find("Code").text(); 
 
     name = $(this).find("Name").text(); 
 

 
     $("#variable").append("<option data-value='" + code + "'>" + name + "</option>") 
 
     }); 
 
    }, 
 
    error: function(XMLHttpRequest, textStatus, errorThrown) { 
 
     console.log(errorThrown); 
 
    } 
 
    }); 
 
}

標準

請注意[MPO]:如果服務接受JSONP,我會建議使用而不是CORS,但是要麼會給出相同的最終結果。

+0

感謝您的詳細解釋。我需要包含任何特定的Base64編碼庫嗎? – Forkmohit

+0

abc123-我能夠完成這項工作。十分感謝。 – Forkmohit

1

嗨擔保,如果要覆蓋htpasswd的安全,我想你只需要寫你的網址,如:

var serviceURL = "http://admin:[email protected]"; 
+0

已經試過了,不起作用。此外,服務由Spring安全保障。 – Forkmohit

+0

哦,我看到了,對不起,我不知道春季安全,所以無法測試它... –