jQuery API不使用JavaScript
使用Mashape API进行随机引用,但单击时不会发生任何事情。下面是JS和HTML。JS代码有什么问题吗?当我点击按钮时,什么也没发生。引号未出现在jQuery API不使用JavaScript,javascript,jquery,ajax,mashape,Javascript,Jquery,Ajax,Mashape,使用Mashape API进行随机引用,但单击时不会发生任何事情。下面是JS和HTML。JS代码有什么问题吗?当我点击按钮时,什么也没发生。引号未出现在div中。谢谢 $('#getQuote').click(function (){ $.ajax({ headers: { 'X-Mashape-Key': 'nrXbQkfuWEmshxvDCunSMptEn0M0p1jHWCijsnX9Ow18j8TXus',
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>
$('#getQuote')。单击(函数(){
$.ajax({
标题:{
“X-Mashape-Key”:“NRXBQKFUWEMSHSXVDCUNSMPTEN0M0P1JHWCIJSNx9OW18J8TXUS”,
“内容类型”:“应用程序/x-www-form-urlencoded”,
“接受”:“应用程序/json”
},
方法:'POST',
数据类型:“json”,
网址:'https://andruxnet-random-famous-quotes.p.mashape.com/',
成功:功能(响应){
var ape=JQuery.parseJSON(响应)
var quoteText=ape.quote;
var quoteAuthor=ape.author;
$(“.quote”).html(quoteText);
$(“.author”).html(quoteAuthor);}
});
});
引用
作者
获得报价
防止默认点击事件,删除数据解析:
$(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);}
});
});
});
防止默认的点击事件,删除数据解析:
$(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);}
});
});
});
jquery足够智能,可以自行解析json响应,所以您需要删除解析函数,一切正常:)
jquery足够智能,可以自行解析json响应,所以您需要删除解析函数,一切正常:)
有控制台错误吗?您的代码示例不清楚。js应该封装在“script”标记中。你应该把它放在documentready上的一个函数中,以便能够绑定dom元素。你的javascript在按钮出现在html中之前就已经运行了,你应该试着把它放在html加载下面,以便jquery可以绑定到它event@MarioAlexandroSantini很抱歉,这不太清楚,但JS与HTML位于单独的文件中。为了简洁起见,只粘贴了相关部分。有控制台错误吗?您的代码示例不清楚。js应该封装在“script”标记中。你应该把它放在documentready上的一个函数中,以便能够绑定dom元素。你的javascript在按钮出现在html中之前就已经运行了,你应该试着把它放在html加载下面,以便jquery可以绑定到它event@MarioAlexandroSantini很抱歉,这不太清楚,但JS与HTML位于单独的文件中。为了简洁起见,我粘贴了相关的部分。这很有效,现在我只希望知道更多的原因。需要阅读解析文档。非常感谢。如果有人遇到这个问题,请解释JSON解析以及为什么包含它会返回null。这是有效的,现在希望我知道更多的原因。需要阅读解析文档。非常感谢。如果有人遇到这个问题,请解释JSON解析以及为什么包含它会返回null。