gpt4 book ai didi

javascript - 如何使用AJAX调用API并返回数据?

转载 作者:行者123 更新时间:2023-11-28 16:56:46 27 4
gpt4 key购买 nike

我正在尝试使用 ajax 调用 API。API是https://rapidapi.com/brianiswu/api/genius?endpoint=apiendpoint_d2f41ea4-7d5c-4b2d-826a-807bffa7e78f

我无法让它工作。我在互联网上搜索,但我找不到或理解我的问题的解决方案。这可能很容易,但请帮助我这是我的代码:

<!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>

最佳答案

将您的代码更改为以下代码:

$(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;
});
});

关于javascript - 如何使用AJAX调用API并返回数据?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58936914/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com