Solved

consuming Rest api using JQuery?

Posted on 2013-12-26
7
686 Views
Last Modified: 2013-12-30
Hi,

I am trying to consume the rest api using Jquery.

This rest api requires id/pw to access it.
I am using the following jquery lib.
<script
      src="https://ajax.googleapis.com/ajax/libs/jquery/1.10.2/jquery.min.js"></script>

I have IE 8.
1) window.btoa() is not recognized.
2) Does this code look OK?

$.ajax({ 
   type: "GET", 		
   url: "http://mycompanydomain:9080/bgrestapi/search?pattern=dna",
   dataType: "xml",		
   headers: {"Authorization": make_base_auth(userId, password)},
   //beforeSend: function (xhr){ 
	 //    xhr.setRequestHeader('Authorization', make_base_auth(userId,   password)); 
	   // },
   timeout: 50000, 
   success:function(data,status,response) {
	alert("success");
	//var tmp=response.responseText; 
              // $('#response').text(tmp);

			},
   error: errorCallback }); 


function errorCallback(response, ioArgs){				
	if ( response.status == 0 ){
		alert("Cannot connect to RestAPI (Timeout Expired) !");
	}

	else {
		alert("Error Occurred, HTTP status code: " + ioArgs.xhr.status );
	}
	
}
function make_base_auth(user, password) {
	   alert("IN make_base_auth");
	  var tok = user + ':' + password;
	 var hashName = "Basic " + window.btoa(tok);    <===this btoa() is not recognized
	 //var hash = "Basic " + base64.encode(tok);  <===this base64  is not recognized

	
          alert("OUT make_base_aut---->" + hashName);

	  
	  return hashName;
	}

Open in new window

0
Comment
Question by:dkim18
  • 4
  • 3
7 Comments
 
LVL 52

Expert Comment

by:Scott Fell, EE MVE
ID: 39740113
It looks like you need internet explorer 10 https://developer.mozilla.org/en-US/docs/Web/API/Window.btoa

There are some options to encrypt using js here https://code.google.com/p/crypto-js/ but if you are worried about others seeing your username and password, you would be best off doing this server side.
0
 

Author Comment

by:dkim18
ID: 39740120
How can I add it without encoding it?
Is this right?

 headers: {"Authorization": "Basic username:password"},
0
 

Author Comment

by:dkim18
ID: 39740122
I meant without encrypting it.
0
Master Your Team's Linux and Cloud Stack!

The average business loses $13.5M per year to ineffective training (per 1,000 employees). Keep ahead of the competition and combine in-person quality with online cost and flexibility by training with Linux Academy.

 

Author Comment

by:dkim18
ID: 39740130
I also tried this
data: '{"username: meta, password:data"}',      

and I am getting Cannot connect to RestAPI (Timeout Expired) error.


function errorCallback(response, ioArgs){				
	if ( response.status == 0 ){
		alert("Cannot connect to RestAPI (Timeout Expired) !");
	}

	else {
		alert("Error Occurred, HTTP status code: " + ioArgs.xhr.status );
	}
	
}

Open in new window

0
 
LVL 52

Expert Comment

by:Scott Fell, EE MVE
ID: 39740147
What is the rest api calling for?  Is it calling for a hash or encryption?

If you note the url starts with http not https so it is not going to be sent encrypted, "http://mycompanydomain:9080/bgrestapi/search?pattern=dna"

If your api is calling for a username "foo" and password "bar" then you would need to send over "foo" "bar" and not encrypt it before hand.  Some api's may have you send over a username, password and key which may be a hash of items like username+secret+data.
0
 

Author Comment

by:dkim18
ID: 39740172
Can I ask one more question?

So to test if I can make the connnection to the Rest API web serivce,

I used the Dojo.js & dojox.base64.js to handle the encryption.

Now I am getting connection time out error.
How can I debug this?
Is this username and password issue?

basically, I am testing this from my laptop and the Rest API is sitting on our company domain.
0
 
LVL 52

Accepted Solution

by:
Scott Fell,  EE MVE earned 500 total points
ID: 39740869
It looks like the error is something you are generating.  Use a browser console to debug.  I don't know if ie8 can do that but firefox has firebug and chrome both have a console where you can view any errors and view the full response. In Chrome, if you go to View > Developer Tools then click on the Network tab.  Run your page and you will be able to see the page that you posted to.  If you click on that, you can view what you sent the the response.
0

Featured Post

Master Your Team's Linux and Cloud Stack

Come see why top tech companies like Mailchimp and Media Temple use Linux Academy to build their employee training programs.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Introduction Knockoutjs (Knockout) is a JavaScript framework (Model View ViewModel or MVVM framework).   The main ideology behind Knockout is to control from JavaScript how a page looks whilst creating an engaging user experience in the least …
Nothing in an HTTP request can be trusted, including HTTP headers and form data.  A form token is a tool that can be used to guard against request forgeries (CSRF).  This article shows an improved approach to form tokens, making it more difficult to…
The viewer will learn how to dynamically set the form action using jQuery.
The viewer will learn the basics of jQuery, including how to invoke it on a web page. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery.: (CODE)

777 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question