2015-05-23 54 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

哦,我看到了,对不起,我不知道春季安全,所以无法测试它... –