gpt4 book ai didi

javascript - 调用 API 时 Ajax success() 不起作用

转载 作者:行者123 更新时间:2023-11-29 16:44:35 25 4
gpt4 key购买 nike

按钮的 onClick() 事件没有指向任何地方。ajax 的 success() 可能存在一些问题。我不知道是什么,因为我是新手。

var currentAuthor="";
var currentQuote="";
$(document).ready(function(){
$("#getMessage").on("click",function(){
$.ajax({
header:{
"X-Mashape-Key":"xE5Raw3acMmsh4dpp6HEk5WSbJtTp1X9TL3jsnue3VRzr5vNNa",
Accept: "application/json",
"Content-Type": "application/x-www-form-urlencoded"
},
url:"https://andruxnet-random-famous-quotes.p.mashape.com/?cat=",
success: function(response){
var r=json.parse(response);
currentQuote=r.quote;
currentAuthor=r.author;
$("#author").html(r.author);
}
});
});
});

最佳答案

您遇到了 2 个问题:

  1. 标题应该是标题
  2. 它是 JSON.parse 而不是 json.parse

var currentAuthor = "";
var currentQuote = "";
$(document).ready(function () {
$("#getMessage").on("click", function () {
$.ajax({
headers: {
"X-Mashape-Key": "xE5Raw3acMmsh4dpp6HEk5WSbJtTp1X9TL3jsnue3VRzr5vNNa",
"Accept": "application/json",
"Content-Type": "application/x-www-form-urlencoded"
},
url: "https://andruxnet-random-famous-quotes.p.mashape.com/?cat=",
success: function (response) {
var r = JSON.parse(response);
currentQuote = r.quote;
currentAuthor = r.author;
$("#author").html(r.author);
}
});
});
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<button id="getMessage">Get Message</button>
<p id="author"></p>

关于javascript - 调用 API 时 Ajax success() 不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42375864/

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