Solved

consuming Rest api using JQuery?

Posted on 2013-12-26
7
683 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
How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

 

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

Find Ransomware Secrets With All-Source Analysis

Ransomware has become a major concern for organizations; its prevalence has grown due to past successes achieved by threat actors. While each ransomware variant is different, we’ve seen some common tactics and trends used among the authors of the malware.

Join & Write a Comment

Introduction Chart.js, used properly, can visually add a difference to your charting applications. It engages your visitors and allows them to interact with data they otherwise wouldn't be able to without expensive and complicated systems. For this…
Introduction If you're like most people, you have occasionally made a typographical error when you're entering information into an online form.  And to your consternation, the browser remembers the error, and offers to autocomplete your future entr…
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)

758 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

Need Help in Real-Time?

Connect with top rated Experts

18 Experts available now in Live!

Get 1:1 Help Now