gpt4 book ai didi

javascript - jQuery API 无法与 JavaScript 一起使用

转载 作者:行者123 更新时间:2023-11-28 18:29:43 25 4
gpt4 key购买 nike

使用 Mashape API 进行随机报价,但点击时没有任何反应。下面是JS和HTML。是不是JS代码有问题?当我单击按钮时,什么也没有发生。引用未出现在 div 中。谢谢!

$('#getQuote').click(function (){
$.ajax({
headers: {
'X-Mashape-Key': 'nrXbQkfuWEmshxvDCunSMptEn0M0p1jHWCijsnX9Ow18j8TXus',
'Content-Type': 'application/x-www-form-urlencoded',
'Accept': 'application/json'
},
method:'POST',
dataType: 'json',
url: 'https://andruxnet-random-famous-quotes.p.mashape.com/',
success: function(response) {
var ape = JQuery.parseJSON(response)
var quoteText = ape.quote;
var quoteAuthor = ape.author;
$(".quote").html(quoteText);
$(".author").html(quoteAuthor);}
});
});



<body>
<div class = "quote">quote</div>
<div class = "author">author</div>
<div id="button">
<button id="getQuote">Get Quote</button>
</div>
</body>

最佳答案

防止默认点击事件,去掉数据的解析:

$(function(){
$('#getQuote').click(function (e){
e.preventDefault();
$.ajax({
headers: {
'X-Mashape-Key': 'nrXbQkfuWEmshxvDCunSMptEn0M0p1jHWCijsnX9Ow18j8TXus',
'Content-Type': 'application/x-www-form-urlencoded',
'Accept': 'application/json'
},
method:'POST',
dataType: 'json',
url: 'https://andruxnet-random-famous-quotes.p.mashape.com/',
success: function(response) {
var ape = response//remove the parsing
var quoteText = ape.quote;
var quoteAuthor = ape.author;
$(".quote").html(quoteText);
$(".author").html(quoteAuthor);}
});
});
});

https://jsfiddle.net/cyLyn8ba/

关于javascript - jQuery API 无法与 JavaScript 一起使用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38320517/

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