我有一个搜索框,用户在其中输入ISBN编号,提交时,div会显示来自Google Books API的信息。此信息以标准格式从 JSON 文件中检索。我能够在div 中毫无问题地显示标题、副标题、作者和描述。
然后我有一个"添加到库"按钮,它应该将所有这些信息发送到数据库,我的问题是所有字段都除了作者之外发送。不是发送作者姓名,而是将单词"Array"发送到数据库。
我获取要发送的作者姓名的唯一方法是在我的 Ajax 数据对象的末尾添加 [0],但这只发送第一作者的姓名(如果有 3 位作者,则省略两个)。
更新 - 它似乎与 JSON 相关,如果我将我的 Ajax 数据更改为"作者"、"行业标识符"或"类别"以外的任何内容,它就可以工作。是因为它们包含一个列表而不是单个字符串吗?
.JS
$(document).ready(function() {
$('#submit').click(function(ev) {
ev.preventDefault();
var isbn = $('#isbn_search').val(); //get isbn direct from input, no need for php
var url='https://www.googleapis.com/books/v1/volumes?q='+isbn;
$.getJSON(url,function(data){
$('.result').empty();
$.each(data.items, function(entryIndex, entry){
var html = '<div class="results well">';
html += '<h3>' + entry.volumeInfo.title + '</h3>';
html += '<h5>' + entry.volumeInfo.subtitle + '</h5>';
html += '<p>' + entry.volumeInfo.authors + '</p>';
html += '<p>' + entry.volumeInfo.description + '</p>';
$('.result').append(html);
});
});
});
});
阿贾克斯
$.ajax({
type: 'POST',
url: 'addIsbnScript.php',
data: {
'isbn' : isbn,
'title' : entry.volumeInfo.title,
'subtitle' : entry.volumeInfo.subtitle,
'authors' : JSON.stringify(entry.volumeInfo.authors),
'description' : entry.volumeInfo.description
},
success: function () {
$("#add").prop('disabled', true);
}
});
.PHP
$isbn = $_POST['isbn'];
$title = $_POST['title'];
$subtitle = $_POST['subtitle'];
$authors = $_POST['authors'];
$decoded_authors = json_decode($authors);
print_r($decoded_authors);
$description = $_POST['description'];
$query = $conn->prepare("INSERT INTO `isbn` (isbn_num,title,subtitle,authors,description) VALUES (?,?,?,?,?)");
$query->bind_param('issss',
$isbn,
$title,
$subtitle,
$decoded_authors,
$description
);
杰伦
"volumeInfo":{
"title":string,
"subtitle":string,
"authors":[
string
],
"publisher":string,
"publishedDate":string,
"description":string,
"industryIdentifiers":[
{
"type":string,
"identifier":string
}
],
"pageCount":integer,
"dimensions":{
"height":string,
"width":string,
"thickness":string
},
"printType":string,
"mainCategory":string,
"categories":[
string
],
"averageRating":double,
"ratingsCount":integer,
"contentVersion":string,
"imageLinks":{
"smallThumbnail":string,
"thumbnail":string,
"small":string,
"medium":string,
"large":string,
"extraLarge":string
},
"language":string,
"previewLink":string,
"infoLink":string,
"canonicalVolumeLink":string
},
请原谅代码,如果它非常业余,因为我是JS的新手,这只是为了个人发展。
您需要在客户端对其进行解码才能将数组传递到服务器端。像这样:
$.ajax({
type: 'POST',
url: 'addIsbnScript.php',
data: {
'isbn' : isbn,
'title' : entry.volumeInfo.title,
'subtitle' : entry.volumeInfo.subtitle,
'authors' : JSON.stringify(entry.volumeInfo.authors),
'description' : entry.volumeInfo.description
},
success: function () {
$("#add").prop('disabled', true);
}
});
然后在你的PHP文件(addIsbnScript.php)
<?php
$authors = $_POST['authors'];//'["Chris Cleave","Philippa Gregory","Sarah Pekkanen"]'
$decoded_authors = json_decode($authors);
print_r($decoded_authors);// Array ( [0] => Chris Cleave [1] => Philippa Gregory [2] => Sarah Pekkanen)
?>
希望这有帮助。干杯