Solved

consuming Rest api using JQuery?

Posted on 2013-12-26
7
688 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
Space-Age Communications Transitions to DevOps

ViaSat, a global provider of satellite and wireless communications, securely connects businesses, governments, and organizations to the Internet. Learn how ViaSat’s Network Solutions Engineer, drove the transition from a traditional network support to a DevOps-centric model.

 

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!

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.

Question has a verified solution.

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

Suggested Solutions

Introduction A frequently asked question goes something like this:  "I am running a long process in the background and I want to alert my client when the process finishes.  How can I send a message to the browser?"  Unfortunately, the short answer …
JavaScript can be used in a browser to change parts of a webpage dynamically. It begins with the following pattern: If condition W is true, do thing X to target Y after event Z. Below are some tips and tricks to help you get started with JavaScript …
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)
The viewer will learn the basics of jQuery including how to code hide show and toggles. 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…

829 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