开发者

Rest api authentication in javascript

I am trying to use a REST api with JavaScript and can't seem to get it to work. I have a Perl equivalent which is as follows:

my $req = HTTP::Request->new开发者_如何转开发(POST => 'https://someurl');
$req->content_type('application/xml');
$req->headers(Accept => 'application/xml; charset=utf-8');
$req->authorization_basic('username','password');
$req->content($body);

This is the request. How do I do this in JavaScript, I looked online and couldn't find anything obvious for syntax.


It will useful to user jquery framework and make ajax call like

    $.ajax({
'url': '/someurl',
async:false,
'beforeSend': function(xhr) {
     xhr.setRequestHeader("Authentication", "Basic " + encodeBase64(username + ":" + password) 
}});


You will need to use the .ajaxmethod. See http://dothow.blogspot.com/2009/05/http-basic-authentication-with-jquery.html for an example of basic authorization from jquery.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜