[Last Call] Learn how to a build a cloud-first strategyRegister Now

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 728
  • Last Modified:

consuming Rest api using JQuery?

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
dkim18
Asked:
dkim18
  • 4
  • 3
1 Solution
 
Scott Fell, EE MVEDeveloperCommented:
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
 
dkim18Author Commented:
How can I add it without encoding it?
Is this right?

 headers: {"Authorization": "Basic username:password"},
0
 
dkim18Author Commented:
I meant without encrypting it.
0
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
dkim18Author Commented:
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
 
Scott Fell, EE MVEDeveloperCommented:
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
 
dkim18Author Commented:
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
 
Scott Fell, EE MVEDeveloperCommented:
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

VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

  • 4
  • 3
Tackle projects and never again get stuck behind a technical roadblock.
Join Now