gpt4 book ai didi

javascript - JSONP 与 ISBNdb

转载 作者:行者123 更新时间:2023-11-28 01:09:48 24 4
gpt4 key购买 nike

我试图在从 ISBNdb API 提交用户表单时提取图书数据。 。我根据需要在 url 中使用 JSON。截至目前,我正在使用表单按 books category 进行搜索(参见下页大约一半)。当我输入搜索词时,我在控制台中收到 2 个错误:

Resource interpreted as Script but transferred with MIME type text/plain: "http://isbndb.com/api/v2/json/J6FR9HT6/books?jsoncallback=jQuery1111037271023145876825_1404698815454&q=science&_=1404698815455".

jquery.min.js:4 Uncaught SyntaxError: Unexpected token :

我看到了我所期待的 q=science,但是 jQuery 似乎添加的其他内容是什么?非常感谢任何帮助!

<!DOCTYPE html>
<html lang="">
<head>

<meta charset="utf-8">
<title></title>

<script src="//ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script>

<script>

$(document).ready(function() {

$('form').submit(function (event) {
event.preventDefault();
var searchTerm = $('#search').val();
// the AJAX part
var isbndbAPI = 'http://isbndb.com/api/v2/json/J6FR9HT6/books?jsoncallback=?';

var bookOptions = {
q: searchTerm
};
function displayBookData(data) {
var bookHTML = '<ul>';
$.each(data.data,function(i,book) {
bookHTML += '<li>';
bookHTML += book.title;
bookHTML += '</li>';
}); // end each
bookHTML += '</ul>';
$('#book-results').html(bookHTML);
}
$.getJSON(isbndbAPI, bookOptions, displayBookData);

}); // end submit

}); // end ready

</script>

</head>
<body>


<form>
<label for="search">Type a search term</label>
<input type="search" name="search" id="search">
<input type="submit" value="Search" id="submit">
</form>

<div id="book-results"></div>

</body>
</html>

更新

似乎可以正常接收 JSON,并且我可以在控制台中看到结果。仍然很困惑,因为这对我来说都是新的。另外,我也可能无法将其正确显示为 HTML。

enter image description here enter image description here

最佳答案

原始帖子中的 url 未包含 api 所需的参数:

错误:“‘query’或‘q’是必需参数”

参见ISBNdb API -- Version 2

Request URL: http://isbndb.com/api/v2/xml/mykey/books?q=science

尝试

$.getJSON("https://query.yahooapis.com/v1/public/yql?q=select"
+"* from json where url='http://isbndb.com/api/v2/json/J6FR9HT6/books?q=science'"
+"&format=json&diagnostics=true&callback=?"
, function(data, textStatus, jqxhr) {
console.log(data.query.results.json.data);
$.each(data.query.results.json.data, function(index, value) {
$("<li>", {"text" : value.title +", "
+ (value.author_data
? (value.author_data.name
? value.author_data.name
: value.author_data.id)
: void(0)) }).appendTo("ul");
});
})
.fail(function(jqxhr, textStatus, errorThrown) {
console.log(textStatus, errorThrown);
});

jsfiddle http://jsfiddle.net/guest271314/bZBLf/

关于javascript - JSONP 与 ISBNdb,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24602341/

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