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 .ajax
method. See http://dothow.blogspot.com/2009/05/http-basic-authentication-with-jquery.html for an example of basic authorization from jquery.
精彩评论