0

Im trying to call an API using ajax. The api is https://rapidapi.com/brianiswu/api/genius?endpoint=apiendpoint_d2f41ea4-7d5c-4b2d-826a-807bffa7e78f

I cant make it work.I searched in internet but i cant find or understand a solution for my problem. Its easy probably, but please help me This is my code:

<!DOCTYPE html>
<html lang="en">
<head>
	<meta charset="UTF-8">
    <title>MusicApp</title>
    <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.4.0/css/bootstrap.min.css">

    <script src="https://code.jquery.com/jquery-3.4.1.js"></script>

</head>

<body>
	<form>
		<input type="text" id="inputText">
		<input type="submit" name="submit" id="submitBtn">
	</form>

	<div id="text"></div>
</body>

<script>
	
	$(document).ready(function(){
		var api="1f57380a81msh394cf453f4d1e73p1a0276jsnab0cd43f0df7";

		$('#submit').click(function(){
			artist=$('#inputText').val();

			$.ajax({
				method:"GET",
				url: "https://genius.p.rapidapi.com/search?q=" + artist + "&appid=" + api,
				success:function(resp){
					alert("successfully");
				},
				error:function(){
					alert("Something went TEREBLY WRONG!!!! \nYOU BROKE IT!");
				}
			})
		})
	})
</script>
</html>

3
  • Inside the success function, find out what resp is. Do console.log(resp); Commented Nov 19, 2019 at 15:01
  • The api you linked has authorization headers required. Commented Nov 19, 2019 at 15:02
  • well you need to cancel the form submission or the form is going to submit while you are making the Ajax call. Commented Nov 19, 2019 at 15:11

3 Answers 3

2

Change your code to the one below :

$(document).ready(function(){
    var api="1f57380a81msh394cf453f4d1e73p1a0276jsnab0cd43f0df7";

    $('#submitBtn').click(function(){
        artist=$('#inputText').val();

        $.ajax({
            method:"GET",
            url: "https://genius.p.rapidapi.com/search?q=" + artist + "&appid=" + api,
            success:function(resp){
                alert("successfully");
            },
            error:function(){
                alert("Something went TEREBLY WRONG!!!! \nYOU BROKE IT!");
            }
        });
        return false;
    });
});
Sign up to request clarification or add additional context in comments.

3 Comments

As I am not authorized to access the API ,I got a 401 in return. But the call is executing.
Thank you! And how to make the result to show up in the div that i made?
You can add it in the success callback - $('#text).value = resp
1

You forgot an essential thing that is preventing you from testing and developing your script: preventing form submit. Do it with e.preventDefault();:

$(document).ready(function() {
  var api = "1f57380a81msh394cf453f4d1e73p1a0276jsnab0cd43f0df7";

  $('#search-form').submit(function(e) {
    e.preventDefault();
    var artist = $('#inputText').val();

    $.ajax({
      method: "GET",
      url: "https://genius.p.rapidapi.com/search?q=" + artist + "&appid=" + api,
      success: function(resp) {
        console.log("successfully");
        console.log(resp);
      },
      error: function(resp) {
        console.log("Something went TEREBLY WRONG!!!! \nYOU BROKE IT!");
        console.log(resp);
      }
    })
  })
})
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.4.0/css/bootstrap.min.css">
<script src="https://code.jquery.com/jquery-3.4.1.js"></script>

<form id="search-form" class="form-inline">
  <input type="text" id="inputText" class="form-control">
  <input type="submit" name="submit" id="submitBtn" class="btn btn-default">
</form>

<div id="text"></div>

Comments

0

Your token is not valid it gives an unauthorised error.

Once you get the valid one it will work.

	$(document).ready(function(){
		var api="1f57380a81msh394cf453f4d1e73p1a0276jsnab0cd43f0df7";

		$('form').submit(function(event){
                        event.preventDefault();
			var artist=$('#inputText').val();

			$.ajax({
				url: "https://genius.p.rapidapi.com/search?q=" + artist + "&appid=" + api,
				success:function(resp){
					alert(resp);
				},
				error:function(e){
					$('#text').text(e.statusText);
				}
			})
		})
	})
<!DOCTYPE html>
<html lang="en">
<head>
	<meta charset="UTF-8">
    <title>MusicApp</title>
    <script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.0/jquery.min.js"></script>
</head>
<body>
	<form>
		<input type="text" id="inputText">
		<input type="submit" name="submit" id="submitBtn">
	</form>
	<div id="text"></div>
</body>

</html>

2 Comments

Thank you! How to take the valid one? Sorry for the stupid questions.Its very new to me
Go to docs.rapidapi.com/docs/keys to learn how to get your API application key. Not sure is it free or you have to pay for testing. Probably you know that better.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.