为了账号安全,请及时绑定邮箱和手机立即绑定

使用jQuery / Javascript将搜索结果保存在Mysql中

使用jQuery / Javascript将搜索结果保存在Mysql中

12345678_0001 2021-05-12 17:21:40
我正在尝试将Google的API搜索结果保存在localhost的Mysql数据库中。我的问题我正在使用一个for循环来展示书籍研究的所有可能结果,例如“哈利·波特”。我使用AJAX进行此操作,然后循环结果。主要问题是,如果要保存其中一个结果,保存的只是最后一个,因为每个变量在每个循环中都会被覆盖。这是代码,saveBook.php只是一个.php文件,我在其中进行插入查询。$.ajax({        url:"https://www.googleapis.com/books/v1/volumes?q=" + search,        dataType: "json",        type: 'GET',        success: function(data){            for(i=0;  i < data.items.length; i++)            {               title = $( '<p> ' + data.items[i].volumeInfo.title + '</p>');               author = $('<p> ' + data.items[i].volumeInfo.authors + '</p>');               img = $('<img><a href=' + data.items[i].volumeInfo.infoLink +                        '><br></br><button>Read More</button></a>' );               url= data.items[i].volumeInfo.imageLinks.thumbnail;               save = $('<br><button onclick="save()">Save</button></br>                         </br>');          img.attr('src',url);          title.appendTo("#result");          author.appendTo("#result");          img.appendTo("#result");          save.appendTo("#result");        }      },    });    }     }    function save(){        $.ajax({            type:'POST',            data: {                            url: url,                            title: title,                            author: author,                        },                         url: "saveBook.php",                        success: function(data){                            alert("Success!");                            location.replace("home.php");            }    })}另一个问题是:我应该如何使用此JSONdata: {          url: url,          title: title,          author: author,      }, 实际返回带有标题和作者的字符串,因为url已正确保存。
查看完整描述

2 回答

?
紫衣仙女

TA贡献1839条经验 获得超15个赞

我已经将变量“ save”声明为全局数组,然后,我对每个字符串进行了适当的转义。现在一切正常!非常感谢你。

save[i] = $('<button onclick="save((\'' + data.items[i].volumeInfo.authors + '\'),(\'' + data.items[i].volumeInfo.title + '\'),(\'' + data.items[i].volumeInfo.infoLink + '\'))">Save</button><br><br>')



查看完整回答
反对 回复 2021-05-27
  • 2 回答
  • 0 关注
  • 130 浏览
慕课专栏
更多

添加回答

举报

0/150
提交
取消
意见反馈 帮助中心 APP下载
官方微信