Solved

consuming Rest api using JQuery?

Posted on 2013-12-26
7
704 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 4
  • 3
7 Comments
 
LVL 53

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
MIM Survival Guide for Service Desk Managers

Major incidents can send mastered service desk processes into disorder. Systems and tools produce the data needed to resolve these incidents, but your challenge is getting that information to the right people fast. Check out the Survival Guide and begin bringing order to chaos.

 

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 53

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 53

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

Get 15 Days FREE Full-Featured Trial

Benefit from a mission critical IT monitoring with Monitis Premium or get it FREE for your entry level monitoring needs.
-Over 200,000 users
-More than 300,000 websites monitored
-Used in 197 countries
-Recommended by 98% of users

Question has a verified solution.

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

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…
Today, the web development industry is booming, and many people consider it to be their vocation. The question you may be asking yourself is – how do I become a web developer?
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…

707 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