So I'm creating a GET request to this API: https://immense-depths-6983.herokuapp.com/search/.
A sample URL to test in your browser is: https://immense-depths-6983.herokuapp.com/search?search=94305
The username is "admin" and password is "password".
Running the sample URL in the browser gives me a popup asking for the authentication and upon entering it correctly, I get back a JSON response text.
However, when I run this on code, it doesn't work. I've scoured the web for answers and each solution doesn't work. I've tried admin:password@url, passing in the username and password as data parameters in the GET request, and even using the BTOA hash thing to pass that to the web server. Alas to no results. I've attached my code I'm using right now - hope some good soul can help me out of this problem.
---------CODE--------------
render() {
var url = this.props.url;
var zip = '94305';
// var zip = this.props.zip;
// create the prop type later with built in functionality
var query = zip + this.props.query;
var username = "admin";
var password = "password";
var ret_data = textbookApi(url, query, username, password);
console.log("after");
// if data is null, ignore
// else
// this.setState({textbooks: ret_data});
return (
<div>
</div>
);
}
}
export default Search;
function makeBaseAuth(user, pswd) {
var token = user + ':' + pswd;
var hash = "";
if (btoa) {
hash = btoa(token);
}
return "Basic " + hash;
}
function textbookApi(url_link, query, username, password) {
// console.log("INSIDE");
console.log(makeBaseAuth(username, password));
$.ajax({
type: "GET",
url: url_link,
data: {
'search' : query
},
beforeSend: function (xhr) {
xhr.setRequestHeader('Authorization', makeBaseAuth(username, password));
},
success: function(data) {
console.log("WORKS!!!!!!!!!!!!!!!!!!!!!!!!!");
console.log(data);
return data;
}.bind(this),
error: function(xhr, status, err) {
console.error(url_link, status, err.toString());
}.bind(this)
});
}